Difference Between List & Tuple in Python
Hi! This short tutorial will show you the difference between list and tuple in the Python programming language.
Here is a quick overview:
Let’s get into the discussion and Python code!
Python List
A Python list is used to store different data types, like strings, integers, and object, in a variable. Python lists are mutable, which means they can be altered or modified. Below are examples of Python lists:
# list of integers int_list = [1,2,3,4,5,6,7] print(int_list) # [1, 2, 3, 4, 5, 6, 7] # list of strings str_list = ["books", "shoes", "bags", "shirts", "belts"] print(str_list) # ['books', 'shoes', 'bags', 'shirts', 'belts'] # list of object class MyPet: def __init__(self, pet, age): self.pet = pet self.age = age my_list = [] my_list.append(MyPet("Spaniel",3)) my_list.append(MyPet("Cat",5)) my_list.append(MyPet("Pig",4)) my_list.append(MyPet("Hamster",2)) my_list.append(MyPet("Cockatoo",3)) # for loop to print object list for l in my_list: print(f"{l.pet}({l.age})") # Spaniel(3) # Cat(5) # Pig(4) # Hamster(2) # Cockatoo(3)
Since Python is an object-oriented programming language, everything in it – lists, dictionaries, tuples, sets, functions, etc. – is regarded as an object that belongs to a class.
We can also create a Python list using the list() function:
# list of integers int_list = list((1,2,3,4,5,6,7)) print(int_list) # [1, 2, 3, 4, 5, 6, 7] # list of strings str_list = list(("books", "shoes", "bags", "shirts", "belts")) print(str_list) # ['books', 'shoes', 'bags', 'shirts', 'belts']
In the examples above, we parsed a tuple to the list constructor, which returned a list of integers and strings.
Attributes of Python List
Below are the attributes of Python list:
- A list is an ordered collection of elements, where each element has a specific index starting from 0.
- Lists are mutable, which means you can add, remove, or modify elements after the list is created.
- The elements in a list can be accessed by their index values.
- The indexes of a list are always integers.
- A list can hold duplicate values.
Python Tuple
A Python tuple is a collection of Python objects that are ordered and can’t be altered or modified. Tuples are typically written with round brackets (). Below are examples of tuples:
# integer tuple int_tuple = (1,2,3,4,5,6,7) print(int_tuple) # (1, 2, 3, 4, 5, 6, 7) # string tuple str_tuple = ("toyota","mazda","mercedes","honda","renault","kia") print(str_tuple) # ('toyota', 'mazda', 'mercedes', 'honda', 'renault', 'kia') # nested tuple nest_tuple = ((1,2,3),(4,5,6),(7,8,9)) print(nest_tuple) # ((1, 2, 3), (4, 5, 6), (7, 8, 9))
We can also use the tuple() function to create a tuple from a list:
my_list = [15,25,30,35] my_tuple = tuple(my_list) print(my_tuple) # (15, 25, 30, 35)
Attributes of Python Tuples
Below are the attributes of Python tuples:
- Tuples are immutable, which means once a tuple has been created, its values cannot be changed or modified.
- A tuple can contain items of any data type, including numbers, strings, lists, or even other tuples.
- The items in a tuple are ordered and can be accessed by their index value.
- A tuple can contain other tuples as well as other data types, allowing for nested data structures.
- Like lists, tuples support slicing and concatenation operations.
- Since tuples are immutable, they use less memory than lists.
So, while lists and tuples may look alike, there are some fundamental differences between both data structures, which make them have different capabilities and, therefore, different applications.
I do hope this tutorial has successfully helped you to understand the differences between lists and tuples in Python.
Video, Further Resources & Summary
Do you need more explanations on the differences between lists and tuples in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail the differences between lists and tuples in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Check if List of Lists is Empty in Python (2 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Convert List to Matrix & Vice-Versa in Python (Examples)
- Access Elements in List within Dictionary in Python (2 Examples)
- Learn Python Programming
This post has shown the differences between lists and tuples in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter