Data Structure means data stored in some structured format. How will we store strings, integers, and booleans in a data structure? Here we will see List, Dictionary, Tuple, and Set data structures in details.
Data structures can be treated as containers.
List
It is a dynamic array. An array is a collection of data that is stored consecutively. In Java or other languages, for a normal array, we have to specify the size to refer to the data. Every array has an index that points to the value of the array. The index starts from 0.
num[0] = 1
num[1] = 10
etc.
An array will maintain the data insertion order.
Dynamic array means allocating more space at runtime.
We can store heterogeneous data in the list, which means storing different data types or structures inside a single sequential collection.
E.g.
nums = [2, 3, 6, 8, 0, 1]
nums[0] - access the value at index 0
len(nums) - find the length of the list
nums[-1] - access the last item
nums.append(5) - add the new value at the end
nums.clear() - empty the list
nums.count(5) - count the number of occurrences of the particular value in the list
nums.reverse() - reverse the entire list
nums.sort() - sort the values in the list
nums.insert(0, 10) - insert the value 10 at index 0
These are the functions that can be used with a list.
Dictionary
Dictionary means a key-value pair.
Syntax:
dict_name = {key: value, key: value}
student1 = {"name": "some name", "age": 20}
Here, the key is immutable. The key can be either a string, boolean, integer, or tuple.
Generally, to search for a particular value, we need to search all the values one by one. But in a dictionary, by using the key, fetching time will be faster compared to other data structures. We can retrieve the value in O(1) time.
dict.get() - get the value of a key
dict.keys() - list the keys in the dictionary
dict.values() - list the values in the dictionary
dict.items() - list the pairs in the dictionary
These are the functions associated with a dictionary.
Tuple
An analogy for a tuple is taking a screenshot. Once we take the screenshot, we cannot change anything inside it. The same is applicable to a tuple. Once a tuple is created, we cannot change its values. We can delete the tuple by using the del keyword.
Syntax:
tuple_name = (value1, value2, value3)
A tuple is created mainly to preserve values as original and to maintain their integrity. So, it is immutable. Lists and dictionaries are mutable, where we can change the values in them.
Functions can always return a tuple.
Set
Set is a collection of unique elements. Here, duplication of elements is not allowed. To implement this, hashing methodology is implemented internally. It is the same as set operations in mathematics.
Syntax:
set_name = {element1, element2, element3}
Set does not maintain the user-specified order. Even if we try to add duplicates, it will not allow them.
Set will add the new element at any index. That is why it does not have an index.
Some of the methods in Set:
set_name1.union(set_name2) - union between two sets
set_name1.intersection(set_name2) - common elements between two sets
set_name1.issubset(set_name2) - check set1 is subset of set2
set_name1.issuperset(set_name2) - check set1 is superset of set2
set_name1.pop() - remove random element
set_name1.remove(element) - remove specified elements


Top comments (0)