TUPLE UP! – Diving deeper into Lists and Tuples

--Originally published at Coding The Future

Picture by Ed.ward

Greetings everyone! Time to tuple-up! Or is it bucle-up? Who cares... Just make sure to fasten your seatbelt because we are about to begin a journey!

Today, we'll be discussing two very simple yet very powerful types in Python: Lists and Tuples. You may have heard of arrays in other programming languages, but in Python, we have lists and tuples instead, which are in fact, very similar to them.

Let's begging by quickly defining what a list or tuple is. A list is a variable that can store several values, just like it's name suggests. For example, a list of all of your friends' names. A tuple, on the other hand, is a constant that can hold several values, and obviously it remains unchanged throughout the program. For example, a tuple with the months of the year. Let us begin.

Lists

Declaring a new list is quite simple. All you have to do is type the name of the list, followed by the values you would like to assign inside of square brackets. Take a look:

myFriends = ['Daniela', 'Diego', 'Christian', 'Jillian']
myLuckyNumbers = [2, 3, 7, 13, 10, 15]

As you can see above, I declared two lists. The first one, a list that stores strings, and the second one, stores integers. As you may have intuited from the example, you cannot store different data types in the same list.

Each item is stored in an index number of the list, starting from 0. So if I had to chart the second list for example, it would look something like this:

Index Value Stored
0 2
1 3
2 7
3 13
4 10
5 15

Consequently, if you need to work with a specific value stored in n index, you would simply call by writing listname[index]. So if I wanted to print myLuckyNumber[1], number 3 would be printed, because that's the value stored in the corresponding index.

Tuples

Now let's jump into tuples. A tuple works exactly like a list, except that –as I previously mentioned– a tuple stores constants, meaning that once it's declared, it's values cannot change.

To declare a tuple, you need to type its name and declare its values inside of round brackets. Here's a quick example:

myTuple = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

Indexes work in the same way as in lists.

Empty Lists

Many other languages let you declare an empty list with a predefined size. Python does not. Instead, the size of a list is defined with the number of values assigned when declared. But, what if you wanted to create an empty list and add values later? Here's an easy workaround this issue...

In order to create an empty list, you would need to declare a list and populate it with a default value, like 0. But don't worry, if you needed a list of size 100, you don't need to type it 100 times, just use repetition. Take a look:

myEmptyList = [0] * 100

The above list has 100 indexes and each one is populated with the value 0.

That's it for now! If you would like to check more examples, be sure to check out this awesome tutorial by SthurtLow. Stay tuned for more!

@emamex98

comments powered by Disqus