Python List vs Set vs Tuple vs Dictionary Comparison


Python

Introduction

  • Lists : The Lists are the sequence data type in Python that stores the non homogeneous type of data in an ordered manner. This is one of the powerful tool of Python.
  • Tuples : The Tuples are the sequence data type in Python that stores the non homogeneous type of data in an ordered manner. The data once written cannot be modified in the tuples.
  • Set :A Set is an unordered collection of non homogeneous type of data that stores the unique elements.
  • Dictionary : The dictionary are the ordered collection of data from Python 3.7 whereas it was unordered in the earlier versions. It stores the data in the form of key value pair.

 

Python list vs set vs tuple vs dictionary - Detailed Comparison

The table below lists the comparison of list vs set vs tuple vs dictionary.

Lists Tuple Set Dictionary
It is a ordered collection of non homogeneous data. It is a ordered collection of non homogeneous data. It is an unordered collection of non homogeneous data. It is an unordered collection of data in the form of key value pairs.
List is created using [ ] Tuple is created using ( ) Set is created using { } Dictionary is created using { }
Lists are mutable. So, we can update the lists. Tuples are immutable. So, no updation are allowed. Set is mutable. So, we can update the sets. Dictionary is mutable. So, we can update them but keys are not duplicated.
List allows duplicate values. Tuple allows duplicate values. Set does not allow duplicate values. Dictionary does not allow duplicate keys.
Example : l=[1,"Raj",3.5,20000] Example : t =(1,"Raj",3.5,20000) Example : s ={1,"Raj",3.5,20000} Example : l={'eid':1, "name":"Raj","exp":3.5,"salary":20000}
The append() method adds an element at the end of the list. An element cannot be added to the tuple. The add() method adds a given element to a set. The update() method adds new key value pair to the dictionary, if the key does not exists. However, if the key exists, it will updates the value of given key with the new value.
The pop() method returns and removes the item at the given index from the list. You cannot remove an element from the tuple. The pop() method will return and remove a random item from the set. The pop() method returns the value and removes the specified key value pair from the dictionary.
The sort() method sorts the elements of a given list in a specific ascending or descending order. Though tuples are ordered, the elements cannot be sorted. Elements in the set cannot be sorted as they are unordered. orted() method is used to sort the keys in the dictionary by default.
We can use reverse() method to reverse the list. No such method is defined for a tuple. No such method is defined for a set. As the elements are in the form of key value pair, there is no way to reverse the elements.
Lists are used in JSON format and databases. Tuples are used for inserting records in the database through SQL query. Sets are used to search unique elements and perform join operations. Dictionary is used to create a data frame with the lists and is also used in JSON.

 

Python list vs set vs tuple vs dictionary - 1-1 Comparison

Data Type Ordered Iterable Unique Immutable Mutable
List Yes Yes No No Yes
Dictionary No Yes Keys only Keys only Values only
Tuple Yes Yes No Yes No
Set No Yes Yes No Yes
Frozenset No Yes Yes Yes N

 

Examples - Python list vs Set vs Tuple vs Dictionary

Insertion operation

The example below demonstrates the insertion operation on List, tuple, set and dictionary.
Example

# Program to demonstrate list vs set vs tuple vs dictionary
# Initializing data structures
l=[1,2] # List
t=(1,2) # Tuple
s={1,2} # Set
d={"Name":"Raj","Age":25} # Dictionary

# Insertion in List
l.append(10)
l.append(20)
l.append(30)
print("List is",l)

# Insertion in Tuple
print("You cannot insert new elements to the tuple. Tuple is",t)

# Insertion in Set
s.add(10)
s.add(20)
print("Set is",s)

# Insertion in Dictionary
d.update({"Location":"Mumbai"})
d.update({"Experience":7.5})
print("Dictionary is",d)

Output

List is [1, 2, 10, 20, 30]
You cannot insert new elements to the tuple. Tuple is (1, 2)
Set is {1, 2, 10, 20}
Dictionary is {'Name': 'Raj', 'Age': 25, 'Location': 'Mumbai', 'Experience': 7.5}

 

Deletion operation

The example below demonstrates the deletion operation on List, tuple, set and dictionary.

Example

# Program to demonstrate list vs set vs tuple vs dictionary
 # Initializing data structures
l=[1,2,3,4,5] # List
t=(1,2,3,4,5) # Tuple
s={1,2,3,4,5} # Set
d={"Name":"Raj","Age":25} # Dictionary

# Deletion in List
print("Value deleted from the list is",l.pop(2))
print("List is",l)

# Deletion in Tuple
print("You cannot delete an element from the tuple")
print("Tuple is",t)

# Deletion in Set
print("Value deleted from the Set is",s.pop())
print("Set is ",s)

# Deletion in Dictionary
print("Value deleted from the Dictionary is",d.pop("Age"))
print("Dictionary is",d)

Output

Value deleted from the list is 3
List is [1, 2, 4, 5]
You cannot delete an element from the tuple
Tuple is (1, 2, 3, 4, 5)
Value deleted from the Set is 1
Set is  {2, 3, 4, 5}
Value deleted from the Dictionary is 25
Dictionary is {'Name': 'Raj'}

 

Sorting operation

The example below demonstrates the sorting operation on List, tuple, set and dictionary.

Example

# Program to demonstrate list vs set vs tuple vs dictionary
 # Initializing data structures
l=[110,2,43,54,599] # List
t=(110,2,43,54,599) # Tuple
s={110,12,43,54,599} # Set
d={"Name":"Raj","Age":25, "Location":"Mumbai"} # Dictionary

# Sorting in List
print("Unsorted list is ",l)
l.sort()
print("Sorted list is ",l)


# Sorting a Tuple
print("\nUnsorted tuple is ",t)
print("Sorted tuple is ",sorted(t))
print("Original tuple is ",t)

# Sorting a Set
print("\nUnsorted set is ",s)
print("Sorted set is ",sorted(s))
print("Original set is ",s)

# Sorting a Dictionary
print("\nUnsorted dictionary is ",d)
print("Sorted dictionary is ",sorted(d))
print("Original dictionary is ",d)

Output

Unsorted list is  [110, 2, 43, 54, 599]
Sorted list is  [2, 43, 54, 110, 599]

Unsorted tuple is  (110, 2, 43, 54, 599)
Sorted tuple is  [2, 43, 54, 110, 599]
Original tuple is  (110, 2, 43, 54, 599)

Unsorted set is  {43, 12, 110, 54, 599}
Sorted set is  [12, 43, 54, 110, 599]
Original set is  {43, 12, 110, 54, 599}

Unsorted dictionary is  {'Name': 'Raj', 'Age': 25, 'Location': 'Mumbai'}
Sorted dictionary is  ['Age', 'Location', 'Name']
Original dictionary is  {'Name': 'Raj', 'Age': 25, 'Location': 'Mumbai'}

 

Summary

The knowledge of list vs set vs tuple vs dictionary is very useful while working on real time applications. In many situations, we will need to select an appropriate data structure to store the data so as to make an efficient use of it. In this tutorial, we covered the differences and similarity between list, tuple, set, dictionary along with the insertion, deletion and sorting operation on them. We learned in detail about this with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on when to use list vs set vs tuple vs dictionary.

 

References

Python Data structure

 

Further Reading

15+ simple examples to learn Python list in detail
Python dictionary (15 easy examples with syntax)
10+ simple examples to learn python sets in detail
10+ simple examples to learn python tuple in detail

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment