Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
What is List in Python?
1.1.
Implementation
1.2.
Applications of List
2.
What is Tuple in Python?
2.1.
Implementation
2.2.
Applications of Tuple
3.
What is Python Set?
3.1.
Implementation
3.2.
Applications
4.
What is Dictionary in Python?
4.1.
Implementation
4.2.
Applications of Dictionary
5.
Difference Between List, Tuple, Set and Dictionary
6.
Frequently Asked Questions 
6.1.
How can we concatenate two lists?
6.2.
Can we have duplicate elements in a set?
6.3.
Which is the faster list or set?
6.4.
Can you modify a tuple after it has been created?
6.5.
Can a dictionary have multiple values for the same key?
7.
Conclusion
Last Updated: Jun 5, 2024
Easy

Difference Between List, Tuple, Set and Dictionary in Python

Author Rishabh
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

In Python, List, Tuple, Set, and Dictionary are four fundamental data structures used for organizing and storing data. The key difference is, that Lists and tuples are ordered collections in Python, but lists are mutable while tuples are immutable. Sets are unordered collections of unique elements, while dictionaries are key-value pairs for efficient data retrieval. Sets store unique elements, while dictionaries store related pieces of information.

intro image

What is List in Python?

A list is a collection of ordered elements. Lists can be of different data types such as integers, floats, strings etc. Various operations such as adding or removing elements or searching and sorting for elements can be performed in a list in Python.

Implementation

# Create a list of numbers
Python_list = [1, 2, 3, 4, 5]

# Change an item in the list by index
Python_list[2] = 6

# Add an item to the end of the list
Python_list.append(7)

# Remove an item from the list by value
Python_list.remove(4)
   
# Print the list
print(Python_list) 


Output

output 1

Explanation

In the code, we initialized a list [1, 2, 3, 4, 5] and performed operations first. We changed the third element to 6 and then appended 7 to the end of the list, and finally removed element 4 from the list.

Applications of List

  • Lists can be used to store and access sequential data.
  • We can modify elements in a List because Lists are mutable.

What is Tuple in Python?

A tuple is a sequence of elements separated by commas and enclosed in parentheses. Tuples are similar to lists, but they cannot be modified once created. This means that you cannot add, remove or modify elements of a tuple.

Implementation

# Create a list of numbers
Python_tuple = (1, 2, 3, 4, 5)

# Print first three elements of tuple
print(Python_tuple[:3])


Output

output 2

Explanation

In the above code, we have implemented a tuple (1, 2, 3, 4, 5), and we print the first three elements in the tuple i.e., (1, 2, 3).

Let’s try deleting from the tuple. 

# Deleting a tuple
tuple_del = (1, 2, 3, 4, 5)

# Delete tuple using del
del tuple_del
print(tuple_del)


Output

output 3

Explanation

In the above code we have implemented a tuple (1, 2, 3, 4, 5) and we delete the tuple using ‘del’. Therefore we get an error as the tuple does not exist.

Applications of Tuple

  • Unlike Lists, Tuples are immutable and can be used to store dictionary keys.
  • Tuples can return multiple values from a function, such as you can use a tuple to return minimum and maximum values from a list. 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Python Set?

A set is a built-in data structure in Python that represents a collection of unique elements. We can perform different operations on sets, such as union, intersection, difference, and symmetric difference.

Implementation

# Initialize two sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}

# Get the union of the two sets 
union_set = set1 | set2
print("Union of the two sets is:",union_set)

# Get the intersection of the two sets 
intersection_set = set1 & set2
print("Intersection of the two sets is:",intersection_set)

# Get the difference of the two sets 
difference_set = set1 - set2
print("Difference of the two sets is:",difference_set)


Output

output 4

Explanation

In the above code, we have implemented two sets where set1 is {1, 2, 3} and set2 is {2, 3, 4} and performed operations such as the union of the two sets i.e., {1, 2, 3, 4}, the intersection of the two sets i.e., {2, 3} and difference on the two sets i.e., {1}.

Applications

  • Sets can be used to perform various operations such as union, intersection and difference.
  • Sets can be used to remove duplicates from a list.

What is Dictionary in Python?

A dictionary in Python is a collection of key-value pairs, where each key is unique and associated with a value. We can change the values of a dictionary. They are useful for storing and accessing data.

Implementation

# Create a dictionary with some key-value pairs
countries = {"India": "New Delhi", "Australia": "Canberra", "Japan": "Tokyo"}

# Print the dictionary
print(countries['India'])
print(countries['Australia'])
print(countries['Japan'])


Output

output 5

Explanation

In the above code we have implemented a dictionary where the country names are the key {India, Australia, Japan} and the capitals are their values {New Delhi, Canberra, Tokyo}.

Applications of Dictionary

  • Dictionaries can be used to store key-value pairs.
  • Dictionaries are also useful for counting the frequency of elements in a list.

Difference Between List, Tuple, Set and Dictionary

Now we will draw a comparison table between a list, tuple, set and dictionary in Python and discuss about the characteristics of each of them.

List 

Tuple

Set

Dictionary

A list is a collection of ordered elements.

A tuple is a sequence of elements separated by commas and enclosed in parentheses.

A set is a built-in data structure in Python that represents a collection of unique elements.

A Dictionary is a collection of key-value pairs, where each key is unique and associated with a value.

Lists maintain the order of the elements they contain.

Tuples maintain the order of the elements they contain

Sets do not maintain the order of the elements they contain

Dictionaries do not  maintain the order of the elements they contain.

Lists can be accessed by index

Tuples can be accessed by index

Sets cannot be accessed by index

Dictionaries cannot be accessed by index

Lists can be modified by adding or removing elements

Tuples cannot be modified by adding or removing elements

Sets can be modified by adding or removing elements

Dictionaries cannot be modified by adding or removing elements

Lists can contain duplicate elements

Tuples can contain duplicate elements

Sets cannot contain duplicate elements

Dictionaries cannot contain duplicate elements

Lists can be accessed by index

Tuples can be accessed by index

Sets cannot be accessed by index

Dictionaries cannot be accessed by index

 

Frequently Asked Questions 

How can we concatenate two lists?

It is very easy to concatenate two lists using the + operator or extend() method.

Can we have duplicate elements in a set?

No, a set cannot contain duplicate elements. It only stores unique elements. 

Which is the faster list or set?

It depends on the use case. Lists are faster when we need to access elements using their index, while sets are faster when we need to check for the existence of an element.

Can you modify a tuple after it has been created?

No, tuples are immutable and cannot be modified after they are created.

Can a dictionary have multiple values for the same key?

No, each key in a dictionary can only have one associated value.

Conclusion

In this article, we discussed the differences between list, sets, tuples, and dictionaries. You can also read the article Differences between List and Set to improve your knowledge about List and Set.

To learn more, check out our articles:

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Code 360. Also, you can enrol in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Python List Operations
Next article
Lists vs Tuples
Live masterclass