How Mich Money Can One Make In 2022

Tuple Vs List

Introduction

Python has four basic inbuilt data structures, which are Dictionary, Tuple, Lists, and Set. Lists and tuples, which are data structures used in Python, have significant differences between them. These differences make them stand apart and are helpful in using them in appropriate situations. Both lists and tuples hold python objects, which are separated by commas. They vary in declaration, mutability, time, memory consumption etc.

What are Tuples and Lists?

Data structures are used in Python. Tuples and lists are a type of data structure in Python. Both tuples and lists store data in a single variable. The difference is that tuples store the data in a variable enclosed within square brackets whereas lists store data within parentheses. Tuples and lists are used to store multiple values. Lists are more dynamic and tuples are more static.

On further reading, you will find out Tuple Vs List key differences. 

1.Syntax (Tuple Vs List)

There is a difference in the way tuples and lists are declared in Python.

Tuple: Data is stored in a variable enclosed within parentheses.

Eg:  tup1 = (1,2,3,4)

print(tup1)

Output: (1,2,3,4)

Explanation: Here in the above example, we can see that the variable “tup1” holds the data “1,2,3,4” enclosed within parentheses. When we use the print statement to print this tuple, the output is (1,2,3,4).

List: Data is stored in a variable enclosed within square brackets

Eg: lis1 = [5,6,7,8]

print(lis1)

Output: [5,6,7,8]

Explanation: Here in the above example, we can see that the variable “lis1” holds the data “5,6,7,8” enclosed within square brackets. When we use the print statement to print this list, the output is [5,6,7,8].

Note: Thus we see the major difference between a tuple and a list is that the declaration is different. Tuples use parentheses whereas lists use square brackets to store data.

Modifying data 

Let us consider an example of a list

lis2 = [100,200,300]

It should be noted that the index of first element always starts from 0 and not 1.

Now let us change the first element of the list.

lis2[0] = “50”

print(lis2)

Output: [50,200,300]

Explanation:  Thus we see that we can change values in a list.

Let us consider an example of a tuple

tup2 = (25,35,45)

It should be noted that the index of first element always starts from 0 and not 1.

Now let us change the first element of the tuple.

tup2(0) = “15”

print(tup2)

Output: You will get an Error mentioning that the item assignment is not supported by the tuple.

Explanation: Thus we see that we cannot change values in a tuple.

Note: From the above, we can conclude that we can only modify lists and that cannot be done on tuples.

2. Mutable (Lists) vs Immutable (Tuples)

Lists are mutable structures and tuples are immutable structures. Lists can be modified after they have been created whereas tuples cannot be modified after their creation.

3. Memory consumption

We already know that tuples are immutable in the sense they cannot be modified after creation and lists are mutable structures, which can be modified after creation. So in the case of tuples, Python allocates large memory blocks and lists have small memory blocks. Hence, tuples have a small amount of memory when compared to lists so tuples are faster to use than lists when a lot of elements are involved.

4. Length Differences

There is a marked difference in length and size of tuples and lists.

Lists have variable length, but tuples have fixed length.

For example:

lis3 = [100,200,300]

lis.append(“400”)

print(lis1)

Output:

[100,200,300, 400]

Explanation: In order to add the new element “400” to the list, we use the function append(). This is possible only with lists and not possible with tuples. So the size can be changed in the case of lists but not with tuples.

5. Error-Prone

Tuple operations are not prone to errors, but list operations are susceptible to errors. This is because of the mutability factor. We know that tuples are immutable whereas lists are mutable. Therefore, lists are more prone to alterations and unexpected changes whereas tuples are safe.

6. Time Consumption

There is difference between lists and tuples as far as time consumption is concerned. List iterations are more time consuming when compared to tuples, which require less time. Thus, tuple iterations are less time-consuming and faster than lists whereas list iterations are slower than tuples and more time-consuming.

7. Heterogeneous Vs Homogeneous

Lists can store data of various types making it heterogeneous, which is a very important and powerful tool in Python whereas tuples can store only homogenous data i.e. data of the same kind.

Example:

Lis4 = [100, ‘Peter’, 20]

Tup4 = (‘Peter’, ‘Tom’, ‘John’)

8. Methods

Lists have more built-in methods and hence some extra functionalities when compared to tuples. On the other hand, tuples have lesser number of built-in methods and functions.

9. Usage criteria

Tuples are very efficient when accessing elements are involved. They are used in the case of read-only operations whereas lists are useful in the case of insertion and deletion operations.

10. Difference between List and Tuple in Tabular form

Difference between List and Tuple in Tabular form
S.NoListTuple
1Syntax of lists have data elements enclosed within square brackets []Syntax of tuples have data elements enclosed within parentheses()
2Built-in methods and functionalities are more than tuplesBuilt-in methods and functionalities are less than lists
3Structure of lists are mutableStructure of tuples are immutable
4Variable list size and lengthFixed tuple size and length
5Lists consume more memory than tuplesTuples consume less memory than lists
6Lists are useful In insertion and deletion operationsTuples are helpful in read-only operations while accessing elements
7It is easy to modify data in lists after creation.It is not possible to modify data in a tuple after it is created.
8Lists can store different data types making it heterogeneousTuples can only store data of the same kind making it homogeneous
9Iterations in lists are slower than tuplesIterations in tuples are faster than lists
10Lists are more prone to errors when compared to tuplesTuples are comparatively safe and less error-prone when compared to lists

Conclusion

We have dealt with the differences between lists and tuples (Tuple Vs List). It should also be noted that we can also have nested tuples and nested lists. Lists and tuples can be stored in each other. The above are the major differences between tuples and lists making them unique and useful in their own way. Thus lists and tuples in Python are very important data structures thereby making them a powerful tool in Python.

Leave a Comment