The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data types that are used to store a collection of values. The other two built-in types are set and dictionary. These are sequential data types, meaning you can iterate over the elements stored in them. It is so important to be able to differentiate between these data types.
Lists are one of the built-in data types in Python. Lists are primarily used to store multiple values of the same or different data types in a single variable. In other words, Lists are used for storing a collection of data.
Lists don’t have to be homogenous, so they can store different types of values, which is a very powerful feature in a programming language. Lists are dynamically sized arrays, so we can increase or decrease their size per our requirement.
Lists are created using a square bracket [ ], and the data Inside it is separated using a comma (,).
# creating list list_name = [data1, data2, data3,…..]
# creating a list of integer Student_age = [ 15, 20, 30, 40 ] # creating a list of mixed data types like integer, float and string my_var = [ 1, ‘TutorialAndExample’ , 5.6 ]
In the above example, we have created a list with the variable name Student_age and assigned it values which are of the same data type integer. Also, we have created a list of mixed data types with the variable name my_var and assigned it an integer value, a string value and a string value.
Accessing Elements in a List:
We use the index operator [ ] to access elements of a list. To access the first element of a list, we will access it using index 0.
Let’s understand with an example.
# creating lists list_name = [ 1, 2.5, ‘ Hi! ’ ] #accessing the elements of lists print( list_name ) # This line will print 1 print( list_name ) # This line will print 2.5 print( list_name ) # This line will print Hi!
In the above example, we have created a list of name list_name and stored values of mixed data type. After that, we accessed those data types using the output function print and the access operator ( [ ] ).
Python tuples are also used for storing multiple values of one or more than one data type in a single variable. Tuples are used to store a collection of ordered data which can’t be changed. Tuples don’t have to be homogenous, same as a list. Tuples allow duplicate values (more than one similar data) to store in them.
Tuples are created using parenthesis (), and elements are separated using comma (,). However, parentheses are optional in the case of tuples. We can create tuples with or without a pair of parentheses. Creating tuples without parentheses is known as tuple packing.
# creating tuples using parentheses tuple_name = (‘data1’, ‘data2’, ‘data3’, ‘data4’) # creating tuples without using parentheses # tuple packing tuple_name = data1 , data2, data3
# Different types of tuple # empty tuple first_tuple = ( ) print (first_tuple) # tuple of integers first_tuple = ( 1,2,3,4 ) print (first_tuple) # tuple with mixed data types first_tuple = ( 1, ‘A’, 2.5) print (first_tuple) # nested tuple first_tuple = (“TutorialAndExample”, [ 9,10,11 ] , (1,2,3) ) print(first_tuple)
( ) ( 1, 2, 3, 4 ) ( 1, ‘A’, 2.5 ) (‘TutorialAndExample’, [ 9, 10, 11 ], ( 1, 2,3 ))
The above code shows the different ways to create a tuple and print its value.
Difference between List and Tuple
The syntax for creating a list and a tuple are different. Lists are created using square brackets. While on the other hand, tuples are created using parentheses which is optional. For example,
creating_list = [‘a’, ‘b’, ‘c’, ‘d’] creating_tuple = ( ‘a’, ‘b’, ‘c’, ‘d’ )
Mutable Lists and Immutable Tuples
The most important difference between a list and a tuple is that lists are mutable, which means they can be changed or manipulated. In contrast, tuples are immutable, which means they can’t be changed or manipulated once they are created.
Iterating a list is slower and more time-consuming, whereas iterating a tuple is faster.
Lists have more memory consumption compared to tuples. Since lists are mutable, they consume more memory space. Tuples operation has a smaller size than that of lists.
Lists are very useful for insertion and deletion operations, whereas tuples are very useful for read-only operations.
Lists provide more built-in functions as compared to tuples. To get all the associated built-in functions related to lists and tuples, we use the dir([objects]) command.
List elements are more prone to getting errors while tuples operations are safer.