List vs tuple vs dictionary in Python


In python there are different types of data structures. Among them list, tuple, dictionary. All these three are used to store the data in the python programming language.

What is a List?

List is one of the data structures available in python which is used to store multiple values in a single data structure. We can create a list in Python using the square braces[ ]. It is mutable which means that we can modify a list once it is created.

It takes multiple elements of different data types such as int, float, str etc which are separated by comma in an ordered format. The elements in the list can be accessed by using the index values. The negative or positive can be applied to the lists.

Syntax

The following is the syntax for creating the list.

variable_name = [e1,e2,e3]

Where,

  • varibale_name is name of the variable,

  • e1,e2,e3 are the elements of the list

Example

In this example when we pass the elements in the square brackets, then the list will be created.

list = [1,2,3,4,5,6]
print(list)
print(type(list))

Output

[1, 2, 3, 4, 5, 6]
<class 'list'>

Example

If we create the list using the square braces[] and append the values then the list will be created. The following is the code.

list = []
print("The empty list created by square braces:", list)
list.append([10,30,20])
print("The list with the appended values:", list)
print(type(list))

Output

The following is the output of the empty list created and appended the defined values to it.

The empty list created by square braces: []
The list with the appended values: [[10, 30, 20]]
<class 'list'>

What is a Tuple?

Tuple is one of the data structures of the python programming language. It is used to store multiple values separated by comma in an ordered manner. It is represented by brackets (). It is immutable in the sense that once the tuple is created, we cannot modify it.

The elements in the tuple can be int, float, string, binary data types and it allows duplicates of the elements. It uses indexing for accessing the elements. It allows a single element to be in tuple.

Syntax

variable_name = (e1,e2,e3)

Where,

  • varibale_name is name of the variable,

  • e1,e2,e3 are the elements of the tuple

Example

In this example if we pass the values in the braces then the tuple will be created.

tuple = ('a','b',10,30,14,7)
print(tuple)
print(type(tuple))

Output

('a', 'b', 10, 30, 14, 7)
<class 'tuple'>

Example

Here, we created a tuple and when we try to append an element, it will raise an error as the tuple is immutable.

tuple1 = tuple()
print("The empty tuple created using brackets, ",tuple1)
tuple1[0] = 30
print(type(tuple1))

Output

The following is the output of the empty tuple created using the tuple keyword.

('The empty tuple created using brackets, ', ())
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    tuple1[0] = 30
TypeError: 'tuple' object does not support item assignment

Dictionary

Dictionary is the data structure available in python. The dictionaries are also known as associative memories or associative arrays. We can create a dictionary using the curly braces {}. A dictionary stores the data in the form of key and value pairs.

The data from a dictionary can be accessed by the key, whereas in the other data structures we use the index and the data from the value of a particular key can be accessed through index values. We can use any immutable object as a key, which means tuples and strings can be used as keys. The key in the dictionary is unique whereas the values are not.

Syntax

variable_name = {k1:v1,k2,v2}

Where,

  • varibale_name is name of the variable,

  • k1,k2 are the keys of dictionary

  • v1,v2 are the values of the dictionary key.

Example

The dictionary can be created by passing the key and value pairs to the {}, where the key and value are separated by colon.

d = {'a':10,'b':20,'c':30}
print(d)
print(type(d))

Output

{'a': 10, 'b': 20, 'c': 30}
<class 'dict'>

List vs Tuple vs Dictionary

In the above examples, we saw the creation of the list, tuple and dictionary. Now let’s see the differences between the three.

List

Tuple

Dictionary

List contains heterogeneous elements

Tuple contains heterogeneous elements

Whereas a dictionary contains key-value pairs.

A List is represented by [].

A Tuple is represented by ().

A Dictionary is represented by {}.

Lists in Python are mutable.

These are immutable.

These are mutable.

Lists are ordered.

Tuples are unordered.

Dictionaries are ordered.

  • The list and tuple can be created by using the elements without any defining the key whereas the dictionary uses the key and value pairs.

  • If we want to create a group of elements with some key name, then we can go for dictionary as it accepts key and value.

  • When we want to list out few elements and want to make changes later as per our requirement we can go for list.

  • When we want to combine few elements into group and don’t want to apply any changes further then we can go for tuple. Let’s see the combined example of the list, tuple and the dictionary.

Example

Let’s see an example to understand the difference between the list, tuple and the dictionary.

list = ['a',10,'b',0.4,True]
print("List:", list)
tuple = ('a',10,'b',0.4,True)
print("Tuple:", tuple)
dictionary = {'a':True,10:'Ten'}
print("Dictionary:", dictionary)

Output

List: ['a', 10, 'b', 0.4, True]
Tuple: ('a', 10, 'b', 0.4, True)
Dictionary: {'a': True, 10: 'Ten'}

Updated on: 15-May-2023

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements