Python | Difference Between List and TupleList and Tuple in Python are the class of data structure. The list is dynamic, whereas the tuple has static characteristics. Show Tuple is also a sequence data type that can contain elements of different data types, but these are immutable in nature. In other words, a tuple is a collection of Python objects separated by commas. The tuple is faster than the list because of static in nature. Difference Between List and Tuple in Python:
Article Tags :
Difference Between Python
List vs TupleThe table below includes the basic difference between list and tuple in Python.
The following sections provide a detailed version of list vs tuple for better understanding. Difference in syntaxAs mentioned in the introduction, the syntax for list and tuple are different. For example: list_num = [10, 20, 30, 40] tup_num = (10, 20, 30, 40) MutabilityOne of the most important differences between list and tuple is that list is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed. So, some operations can work on lists, but not on tuples. For example, in data science, if a list already exists, particular elements of it can be reassigned. Along with this, the entire list can be reassigned. Elements and slices of elements can be deleted from the list. On the other hand, particular elements on the tuple cannot be reassigned or deleted, but it is possible to slice it, and even reassign and delete the whole tuple. Because tuples are immutable, they cannot be copied. Check out:Python vs Java OperationsAlthough there are many operations similar to both lists and tuples, lists have additional functionalities that are not available with tuples. These are insert and pop operations, and sorting and removing elements in the list. FunctionsSome of the Python functions can be applied to both data structures, such as len, max, min, any, sum, all, and sorted. SizeIn Python, tuples are allocated large blocks of memory with lower overhead, since they are immutable; whereas for lists, small memory blocks are allocated. Between the two, tuples have smaller memory. This helps in making tuples faster than lists when there are a large number of elements. Type of elementsElements belonging to different data types, i.e., heterogeneous elements, are usually stored in tuples. While homogeneous elements, elements of the same data types, are usually stored in lists. But this is not a restriction for the data structures. Similar data type elements can be stored in tuples, and different data type elements can also be stored in lists. LengthLengths differ in the two data structures. Tuples have a fixed length, while lists have variable lengths. Thus, the size of created lists can be changed, but that is not the case for tuples. DebuggingWhen it comes to debugging, inlists vs tuples, tuples are easier to debug for large projects due to its immutability. So, if there is a smaller project or a lesser amount of data, it is better to use lists. This is because lists can be changed, while tuples cannot, making tuples easier to track. Nested lists and tuplesTuples can be stored in lists, and likewise, lists can be stored inside tuples. In nested tuples, a tuple can hold more tuples. And in nested lists, a list can hold more lists. UsesIt is important to understand that there are different cases where it is better to use one of these data structures, such as; using either one depends on the programmer, i.e., choosing one based on whether they want to change the data later or not. Tuples can be used as equivalent to a dictionary without keys to store data. When tuples are stored within lists, it is easier to read data. Read: More types of data structures in Python Python Lists Vs TuplesIn this article we will learn key differences between the List and Tuples and how to use these two data structure. Lists and Tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type including the nothing type defined by the None Keyword. Lists and Tuples are similar in most context but there are some differences which we are going to find in this article. The Key Difference between a List and a TupleThe main difference between lists and tuples is the fact that lists are mutable whereas tuples are immutable. What does that even mean, you say? A mutable data type means that a python object of this type can be modified. An immutable object can’t. Let’s see what this means in action. Let’s create a list and assign it to a variable. >>> a = ["apples", "bananas", "oranges"]Now let’s see what happens when we try to modify the first item of the list. Let’s change “apples” to “berries”. Perfect! the first item of a has changed. Now, what if we want to try the same thing with a tuple instead of a list? Let’s see. >>> a = ("apples", "bananas", "oranges") >>> a[0] = "berries" Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignmentWe get an error saying that a tuple object doesn’t support item assignment. The reason we get this error is because tuple objects, unlike lists, are immutable which means you can’t modify a tuple object after it’s created. But you might be thinking, Karim, my man, I know you say you can’t do assignments the way you wrote it but how about this, doesn’t the following code modify a? >>> a = ("apples", "bananas", "oranges") >>> a = ("berries", "bananas", "oranges") >>> a ('berries', 'bananas', 'oranges')Fair question! Let’s see, are we actually modifying the first item in tuple a with the code above? The answer is No, absolutely not. To understand why, you first have to understand the difference between a variable and a python object. |