Python provides us with a number of in-built data structures such as lists, tuples, sets, and dictionaries that are used to store and organize the data in an efficient manner. In this article, we will learn the difference between them and their applications.
Python List
Python Lists are just like dynamic-sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python.
Applications of Python List
- Used in JSON format
- Useful for Array operations
- Used in Databases
Python Tuple
A Tuple is a collection of Python objects separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike lists that are mutable.
Applications of Python Tuple
- Used to insert records in the database through SQL query at a time.Ex: (1.’sravan’, 34).(2.’geek’, 35)
- Used in parentheses checker
Python Set
A Python Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Python’s set class represents the mathematical notion of a set.
Applications of Python Set
- Finding unique elements
- Join operations
Python Dictionary
Dictionary in Python is an ordered (since Py 3.7) [unordered (Py 3.6 & prior)] collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.
Applications of Python Dictionary
- Used to create a data frame with lists
- Used in JSON
Difference between List, Tuple, Set, and Dictionary
The following table shows the difference between various Python built-in data structures.
List | Tuple | Set | Dictionary |
---|---|---|---|
A list is a non-homogeneous data structure that stores the elements in columns of a single row or multiple rows. |
A Tuple is also a non-homogeneous data structure that stores elements in columns of a single row or multiple rows. |
The set data structure is also a non-homogeneous data structure but stores the elements in a single row. |
A dictionary is also a non-homogeneous data structure that stores key-value pairs. |
The list can be represented by [ ] |
Tuple can be represented by ( ) |
The set can be represented by { } |
The dictionary can be represented by { } |
The list allows duplicate elements |
Tuple allows duplicate elements |
The Set will not allow duplicate elements |
The dictionary doesn’t allow duplicate keys. |
The list can use nested among all |
Tuple can use nested among all |
The set can use nested among all |
The dictionary can use nested among all |
Example: [1, 2, 3, 4, 5] |
Example: (1, 2, 3, 4, 5) |
Example: {1, 2, 3, 4, 5} |
Example: {1: “a”, 2: “b”, 3: “c”, 4: “d”, 5: “e”} |
A list can be created using the list() function |
Tuple can be created using the tuple() function. |
A setA dictionary can be created using the set() function |
A dictionary can be created using the dict() function. |
A list is mutable i.e we can make any changes in the list. |
A tuple is immutable i.e we can not make any changes in the tuple. |
A set is mutable i.e we can make any changes in the set, ut elements are not duplicated. |
A dictionary is mutable, ut Keys are not duplicated. |
List is ordered |
Tuple is ordered |
Set is unordered |
Dictionary is ordered (Python 3.7 and above) |
Creating an empty list l=[] |
Creating an empty Tuple t=() |
Creating a set a=set() |
Creating an empty dictionary d={} |
Below is the program for the implementation of list, tuple, set, and dictionary:
Python3
# Python3 program for explaining # use of list, tuple, set and # dictionary # Lists l = [] # Adding Element into list l.append( 5 ) l.append( 10 ) print ( "Adding 5 and 10 in list" , l) # Popping Elements from list l.pop() print ( "Popped one element from list" , l) print () # Set s = set () # Adding element into set s.add( 5 ) s.add( 10 ) print ( "Adding 5 and 10 in set" , s) # Removing element from set s.remove( 5 ) print ( "Removing 5 from set" , s) print () # Tuple t = tuple (l) # Tuples are immutable print ( "Tuple" , t) print () # Dictionary d = {} # Adding the key value pair d[ 5 ] = "Five" d[ 10 ] = "Ten" print ( "Dictionary" , d) # Removing key-value pair del d[ 10 ] print ( "Dictionary" , d) |
Output:
Adding 5 and 10 in list [5, 10]
Popped one element from list [5]
Adding 5 and 10 in set {10, 5}
Removing 5 from set {10}
Tuple (5,)
Dictionary {5: 'Five', 10: 'Ten'}
Dictionary {5: 'Five'}