Python Lists And Tuples
If you have had some programming exposure, I am sure you know about Arrays. Well, Python also has arrays, but they are called Lists and Tuples in Python. They are very much like the plain old arrays that we have known but with some great add-ons. So let us have a look.
Follow full series Learn Python programming.
What are Lists and Tuples
Python Lists and Tuples are collections of elements that are ordered and indexed. They are very similar to an array. However, there is one stark difference between the two. We can modify lists, we can add new elements, and we can remove existing elements.
But in the case of a tuple, modification is not possible. Tuples are permanents since their existence.
Creating Lists and Tuples
The syntax for creating lists and tuples is very much alike, with one small difference. In the code below, lists are declared within square brackets, whereas tuples are declared within parenthesis.
Lists and tuples are both declared by comma-separated values. You can see in the output below that we have tried printing the list and tuple on our console.
Reading Data from Lists and Tuples
However, most of the time, we will need to access individual items from a list or tuple. To do this, we can reference it by its index. All items in lists and tuples begin their indexing from 0. The first item has an index of 0; the second is 1, third is 2, and so on. Try out the following code on your local machines.
You can see in the code above that we have called the item on index 1 from food and 3 from drinks. This is very important to understand because, unlike humans, computers count from 0. It should yield in the following output.
We can also use a for loop to loop through items in a list or tuple.
The above code should result in printing all elements from the drinks tuple.
Modifying Lists and Tuples
We have already made it clear that lists are modifiable, but tuples are not. The following code tries to modify elements from existing lists and tuples.
Running this code, you will receive an error since we have tried to modify an element from a tuple, and it is not allowed. Remove the line that says drinks[0] = “wine” and you should be good to go.
When you being real programming, you will realize that tuples are very rarely used. So most of the interesting functions are for lists. Let us have a look at some of them.
Some Helper Functions
Various functions can help you work around with lists; some of them are below.
We have len() function to count elements in a list. All you need to do is supply a list or tuple object.
The index() function can give you the index of a specified element. Works for both tuples and lists.
There are a lot of other functions that we can use to make working with lists very easy. Assume an existing list food = [“apple”,”onion”,”milk”]. We can use the following functions.
Function | Example | Description |
pop | food.pop(0) | Removes the element at position 0. Element 1 becomes element 0. |
remove | foods.remove(“milk”) | Removes element with value milk. |
insert | food.insert(2,”meat’) | Adds element meat to position 2. Item at position 2 is moved to position 3. |
extend | food.extend(list) | Adds items from another list to the end of the current list. |
append | food.append(“meat”) | Adds item meat to the end of the current list. |
reverse | food.reverse() | Element 0 becomes the last element, 1 becomes seconds last, and so on. |
Conclusion
Lists and tuples are handy in handling the collection of items. Lists are a lot more flexible than tuples since tuples are not modifiable. We have seen some uses of lists in the examples above and a few functions that make working with lists easier. We also have sets and dictionaries that are also used to hold a collection of data. In future articles, we will study more about it.