Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
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.
Difference Between List, Tuple, Set, and Dictionary
Now we will draw a comparison table between a list, tuple, a 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
What is a 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 element,s can be performed in a list in Python.
Implementation
Python
Python
# 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)
You can also try this code with Online Python Compiler
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
Python
Python
# Create a list of numbers Python_tuple = (1, 2, 3, 4, 5)
# Print first three elements of tuple print(Python_tuple[:3])
You can also try this code with Online Python Compiler
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.
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.
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 a 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
Python
Python
# 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'])
You can also try this code with Online Python Compiler
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.
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 conclusion, lists, tuples, sets, and dictionaries are core data structures in Python, each serving different purposes. Understanding their differences in mutability, ordering, and usage helps developers choose the right one for efficient problem-solving.