Lists and Tuples

Both of them actually are lists, but the difference is that a tuple can’t be modified after creation wheras a list can (e.g. add, remove, change values, sorting). They look like the following:

So what is the point in creating a tuple when a list seems to be more flexibe anyway?

One reason is that tuples are generated faster and can be iterated faster than a normal Python list. So if you want to make sure that the list will not be change or you know it doesn’t have to be changed anyway you would use a tuple to increase the performance of your code. An example to use them would be this, but make sure the positions of the numbers won’t change:

As normally lists are used more frequently I want to show you the basic operations as how to add and remove elements from it.

Output:

Soccer supports more the concept of a list, because every player, every coach and every manager is replaceable and will be replaced if the success isn’t enough. An example for the usage of a tuple might be the years of titles won, because they won’t be modified anymore.

To get a broader overview of the topic I can recommend this video:

#Mastery17

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s