Lists and tuples are the dynamic duo of data storage, And with these you can discover the power of ordered collections in Python. Python is a versatile programming language that offers many useful data structures to help manage and organize your code. Among these structures are lists and tuples, two powerful ordered collections that provide different benefits for different programming needs. While lists allow for dynamic modification of the items they contain, tuples Python offer the added benefit of immutability.
In this blog, we'll explore the features of both lists and tuples in Python, so you can start harnessing their power in your own coding projects.
What are Lists in Python? Give Examples
In Python, a list is a collection of items that are ordered and changeable. Lists python are denoted by square brackets [ ] and each item in the list is separated by a comma.
Here are some examples of lists in Python:
Example 1: A list of numbers
This creates a list of 5 numbers, which can be accessed by their index (starting from 0). For example, numbers[0] would return the first item in the list, which is 1.
Example 2: A list of strings
This creates a list of 3 strings, which can also be accessed by their index. For example, fruits[1] would return "banana".
Example 3: A list of mixed data types
This creates a list of 4 items with different data types. The items can still be accessed by their index, and the type of each item can be different. For example, mixed_list[2] would return the floating-point number 3.14.
Example 4: A nested list
This creates a list of lists python, where each inner list contains three numbers. The items in the nested list can be accessed using two indices. For example, nested list[1][2] would return the number 6, which is the third item in the second inner list.
Lists are a very versatile data type in Python and are used in many programming tasks.
What are Tuples in Python?
In Python, a tuple is another type of ordered collection of objects, similar to a list. However, unlike a list, a tuple is immutable, which means that it cannot be changed once it is created. Tuples Python are denoted by parentheses () and each item in the tuple is separated by a comma.
Here are some examples of tuples in Python:
Example 1: A tuple of numbers
This creates a tuple of 5 numbers, which can be accessed by their index (starting from 0), just like a list. However, you cannot modify the items in a tuple once it is created.
Example 2: A tuple of strings
This creates a tuple of 3 strings, which can also be accessed by their index. Again, the items in a tuple cannot be changed once it is created.
Example 3: A tuple of mixed data types
This creates a tuple of 4 items with different data types, which can be accessed by their index. As with the previous examples, the items in the tuple python cannot be changed once it is created.
Example 4: Unpacking a tuple
This creates a tuple of 3 numbers, and then "unpacks" the tuple into three separate variables. After running this code, a would be assigned the value 1, b would be assigned the value 2, and c would be assigned the value 3.
Tuples are useful when you want to store a collection of items that you don't want to change later on. They are often used in cases where you want to return multiple values from a function, or when you want to ensure that certain data is not modified accidentally.
Lists vs Tuples: Differences
In Python, both lists and tuples are used to store collections of data, but there are several differences between them.
1) Mutability:
Lists are mutable, which means you can add, remove, or modify elements after the list is created. Tuples, on the other hand, are immutable, which means you cannot modify the elements once the tuple is created.
How to test whether tuples are immutable and list are mutable
To test whether tuples are immutable and lists are mutable in Python, you can try to modify the elements of the data type and see if it raises an error or not.
For example, let's create a tuple and a list and try to modify them:
This code will raise a TypeError because tuples are immutable and cannot be modified.
Now let's try the same thing with a list:
This code will not raise any error and will print the modified list [6, 2, 3, 4, 5]. This shows that lists are mutable and can be modified.
In summary, you can test whether tuples are immutable and lists are mutable in Python by trying to modify the elements of the data type and seeing if it raises an error or not.
2) Syntax:
Lists are enclosed in square brackets [ ], whereas tuples are enclosed in parentheses ( ). For example:
3) Performance:
Tuples are generally faster than lists because they are immutable. This means that Python can allocate memory for all the elements at once and doesn't need to resize the memory every time an element is added or removed.
How to test if tuples are memory efficient?
To test whether tuples are memory efficient in Python, you can compare the memory usage of a tuple with the memory usage of a list containing the same elements.
The sys.getsizeof() function can be used to get the size of an object in bytes. Let's create a tuple and a list containing the same elements and compare their memory usage:
import sys
# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Getting the size of the tuple in bytes
tuple_size = sys.getsizeof(my_tuple)
# Getting the size of the list in bytes
list_size = sys.getsizeof(my_list)
# Printing the size of the tuple and the list
print("Tuple size: ", tuple_size)
print("List size: ", list_size)
The output of this code will show the size of the tuple and the list in bytes. In general, tuples are more memory efficient than lists because tuples use less memory than lists to store the same elements. This is because tuples are simpler data structures than lists, and do not require as much overhead.
However, the exact amount of memory used by a tuple or a list depends on various factors such as the size and type of the elements in the data structure, the platform on which the code is running, and the version of Python being used. So, while tuples are generally more memory efficient than lists, it's important to test the memory usage of your specific code and data to determine the most efficient data structure to use.
4) Usage:
Lists are commonly used for collections of related data that need to be modified, such as a list of tasks in a to-do list application. Tuples are often used for grouping related data together, such as a latitude and longitude pair.
5) Packing and unpacking:
Tuples allow for packing and unpacking of values. This means you can assign multiple values to a single tuple variable and then unpack them into separate variables later on. For example:
In this example, the values of the tuple are assigned to the variables a, b, and c.
In summary, lists are mutable and commonly used for collections of related data that need to be modified, while tuples are immutable and often used for grouping related data together. Tuples are generally faster than lists and allow for packing and unpacking of values.
Conclusion
Lists and tuples are both powerful ordered collections in Python that can help manage and organize your code. Lists are mutable and allow for dynamic modification of their elements, while tuples are immutable and offer the added benefit of being faster and memory-efficient.
When choosing between lists and tuples, consider your programming needs and whether you require the ability to modify elements or not. Both data structures have their own benefits and are useful in different situations. By understanding the features of both lists and tuples, you can make informed decisions about which data structure to use in your coding projects. So, to learn more about lists and tuples and other functions of Python, do pre-register for the waiting list of the new and improved simulation based Python course at keySkillset.