Published on: 29 Oct, 2021
Updated on: 29 Oct, 2021
Explore Python Data Structures & Its Amazing Features
Here we will be exploring mostly commonly used data structures of Python
Python data structure helps to structure the data so that we can handle and work with it efficiently. Let's consider we have a scenario where we have to store multiple names of programming languages such as Python, Java, Javascript, C
If we try to use the traditional approach we can use different variables to store different names.
For ex.
language_one = 'Python'
language_two = 'Java'
language_three = 'Javascript'
language_four = 'C'
But the problem with this approach, it is gonna store the data at a random location in memory and when we have to access these data back again Python has to scan the whole memory every time to access each language and this leads to slow down of the process and inefficient approach to the programming.
So to overcome these limitations, it is always good to use python data structures. This helps to structure the data within memory in such a way that they are close intact to each other that further helps to work with these data efficiently and in a much faster way.
Python has a rich collection of built-in data structures. These built-in data structures help us to work with collections of data in more efficient ways. And each of the data structures in python has its own unique features.
Here, we will be discussing the most commonly used python data structures.
A. Lists
What is the list?
- A list in python is a collection of data that is stored sequentially just like a string.
- A list can store heterogeneous data unlike string and each data within the list is termed as element or item
- In Python,
square brackets ([])
indicate a list and individual elements in the list are separated by commas. - Here’s a simple example of a list that contains a name of programming language
languages = ['Python', 'Java', 'Javascript', 'C']
- A list can have another list in it and such list is known as nested
languages = ['Python', 'Java', 'Javascript', 'C', ['Python Fundamentals', 'Constructor']]
- A list with no value in it is known as an empty list and can be created using
square brackets []
- Lists are mutable object & can store duplicate values
empty_list = []
List in Action
Let's explore what are the different operations we can perform on list
- Creating a list
languages = ['Python', 'Java', 'Javascript', 'C']
- Adding element at last index
Add element at the end of the list
languages.add('C#')
- Adding element at specific index
Add element at x index in list
languages.insert(3, 'Ruby')
- Accessing individual list element
To access single element from list
print(languages[0])
- Accessing list elements
To access all the elements from list in one go
languages = ['Python', 'Java', 'Javascript', 'C'] for item in languages: print(item) # We can also use in range function for i in range(0, len(languages)): print(languages[i])
- Slicing list elements
To access access specific range of elements
print(languages[2:4])
- Removing single element from list
Remove the first item from the list whose value is equal to x.
languages.remove('Java')
- Removing all elements from list
Remove all the items from the list
languages.clear()
- Modify element from list
To modify the list elements
languages[2] = 'Groovy'
- Sorting the items from list
Sort the items of the list in place
languages.sort()
- Reversing the items from list
Reverse the elements of the list in place.
languages.reverse()
To learn more about data structure - https://docs.python.org/3/tutorial/datastructures.html
B. Tuples
What is the Tuples?
- A tuple in python is a collection of data that is stored sequentially just like a list.
- A tuple can store heterogeneous data and each data within the list is termed as element or item
- In Python,
parenthesis ()
indicate a tuple and individual elements in the tuple are separated by commas. - Here’s a simple example of a tuple that contains a name of programming language
languages = ('Python', 'Java', 'Javascript', 'C')
- A tuple can have another tuple in it and such tuple is known as nested
languages = ('Python', 'Java', 'Javascript', 'C', ('Python Fundamentals', 'Constructor'))
- A tuple with no value in it is known as an empty tuple and can be created using
parenthesis ()
empty_tuple = ()
- Tuples are immutable object unlike lists & can store duplicate values
Tuples in Action
Let's explore what are the different operations we can perform on tuple
- Creating a tuple
languages = ('Python', 'Java', 'Javascript', 'C')
- Accessing individual tuple element
To access single element from list
print(languages[0])
- Accessing tuple elements
To access all the elements from tuple in one go
languages = ('Python', 'Java', 'Javascript', 'C') for item in languages: print(item) # We can also use in range function for i in range(0, len(languages)): print(languages[i])
- Slicing tuple elements
To access access specific range of elements
print(languages[2:4])
To learn more about data structure - https://docs.python.org/3/tutorial/datastructures.html
C. Sets
What is the Set?
- A Set in python is a collection of data that are unordered
- A set can store heterogeneous data like list & tuple and each data within the list is termed as element or item
- In Python,
curly brackets {}
without key-value pair indicate a set and individual elements in the set are separated by commas. - Here’s a simple example of a set that contains a name of programming language
- A list with no value in it is known as an empty list and can be created using
set()
constructor but not{}
- A set itself can be mutable, but the elements contained in the set must be of an immutable type.
- A set is an unordered collection with no duplicate elements
- Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
languages = {'Python', 'Java', 'Javascript', 'C'}
empty_set = set()
We can add tuple to the set but not list & dictionary since they are mutable
x = {43, '34', 'string' (1, 2, 3), 22.6}
Set in Action
Let's explore what are the different operations we can perform on Set
- Creating a Set
languages = {'Python', 'Java', 'Javascript', 'C'}
- Adding element to Set
language.add('Ruby')
- Removing last element from Set
language.pop()
- Removing individual element from Set
language.remove('Ruby')
language.discard('Ruby')
- Removing all elements from Set
language.clear()
- Union of sets
The union of x1 and x2 is a set consisting of all elements in either set.
x1.union(x2)
x1 | x2
- Intersection of sets
The intersection / x1 & x2 return the set of elements common to both x1 and x2
x1.intersection(x2)
x1 & x2
- Difference in sets
The difference / x1 - x2 return the set of all elements that are in x1 but not in x2
x1.difference(x2)
x1 - x2
- Subset of set
Determine whether one set is a subset of the other.
x1.issubset(x2) return True if x1 is a subset of x2
x1 <= x2
There are others methods too that are associated to Sets, to learn more about data structure - https://docs.python.org/2/library/sets.html#sets.Set
D. Dictionaries
What is the Dictionary?
- A dictionary in Python is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key.
- A key’s value can be a number, a string, a list, or even another dictionary. In fact, you can use any object that you can create in Python as a value in a dictionary.
- In Python,
curly brackets {}
with key-value pair indicate a dictionary and individual set of key-value pair in the dictionary are separated by commas. - Here’s a simple example of a dictionary that contains a name of programming language
- A dictionary with no value in it is known as an empty dictionary and can be created using
curly brackets {}
- Dictionaries are mutable object & can store duplicate values but no duplicate key
languages = {'Python':'Python Community', 'Java':'Java Community'}
empty_dict = {}
Dictionary in Action
Let's explore what are the different operations we can perform on dictionary
- Creating a dictionary
{'Python':'Python Community', 'Java':'Java Community'}
- Adding item to dictionary
Add element at the end of the dict
languages['C#'] = 'C# Community'
- Accessing individual dict element
To access single element from dict
print(languages['Python'])
- Accessing dict elements
To access all the elements from dict in one go
languages = {'Python':'Python Community', 'Java':'Java Community'} for item in languages.items(): print(item) for key, value in languages.items(): print(key + ' - ' + value) for key in languages.keys(): print(key) for value in languages.values(): print(value)
- Modify item from dict
To modify the dict key's value
languages['Python'] = 'Py Community'
This will modify the existing key if present else add new key Python with value Py Community - Removing single element from dict
del languages['Java']
To learn more about data structure - https://docs.python.org/3/tutorial/datastructures.html
Here are similar posts
We have more silimar posts that you would love to checkout