Four Built-in Data Structures in Python

Four Built-in Data Structures in Python
Four Built-in Data Structures in Python

In today’s era Python is something that is widely used. It is used in Artificial Intelligence, Machine Learning and a number of websites we use on a daily basis. Youtube, Instagram and Quora use Python. In all these applications where Python is used, one thing is common and it is data. Data in all these applications must be stored and retrieved in an efficient way.

What is Data Structure?

Data Structure helps in organising the data so that it can be stored and retrieved easily. Access
and modification of data are efficient. Data Structure is widely used in almost all aspect of Computer like Operating System, Computer Networks etc.

For Example: We can store a list of items with the help of an array and it can be accessed easily in this way.

Data Structure in Python:

Python has two types of data structures.

  • Build in Data Structure
  • User-Defined Data Structure

Python has implicit Data Structure like Tuple, Dictionary, List and Set. It also allows users to create its own data structure like Stack, Queue, Tree, Linked List and so on. Here is a detailed list of these data structure.

Mutability and Order:
Mutability is something about whether we can change or not an object once it is created. If we
can change it then it is called mutable else it is called immutable. Order is something about whether we can use the index (position)of an object to access the element.

4 Build-in Data Structures

Build-in data structure makes the programming easier and helps the programmer to use them to obtain a faster solution. Almost 80% of real-world data is covered with these four data structures.

Lists: It can store heterogeneous data in sequential order. Every element of the list has an address. Every element has an index which starts from 0 and goes until the last element called positive index. There is a negative index which starts from -1 and it helps to retrieve from last to create a list of square brackets are used.
For Example:

Creating a List:
my list=[1, 5, 3, ’Hello’, ’10’] #creating list with some data
print(my_list)
output:[1, 5, 3, ’Hello’, ’10’]

Adding Elements in List:
It is done through the following functions.
append()
extend()
insert()
We can see the functionality through an example.
For Example:
1.my_list = [1, 2, 3]
output:[1, 2, 3]
2.my_list.append([555, 12]) #add as a single element
output:[1, 2, 3, [555, 12]]
3.my_list.extend([234, ‘more_example’]) #add as different elements
output:[1, 2, 3, [555, 12], 234, ‘more_example’]
4.my_list.insert(1, ‘insert_example’) #add element i and increases size of list
output:[1, ‘insert_example’, 2, 3, [555, 12], 234, ‘more_example’]

Deleting Elements in List:
It can be done through following functions:
remove()
pop()
For example:
1.my_list=[1, 2, 3, ‘example’, 3.132, 10, 30]
2.my_list.remove(‘example’) #removes element with value example
output:[1, 2, 3, 3.132, 10, 30]
3.my_list.pop(1) # removes element with index
output:[1,3, 3.132, 10, 30]


Important Methods Supported By List:
1.clear() Removes all the elements from the list.
2.count() Returns the total number of items passed.
3.sort() Sorts all the elements in ascending order.
4.extend() Adds all the elements of a list to some other list.
5.reverse() Reverses the order of the list.

Dictionaries: They are basically used to store key-value pairs. Like,we store phone numbers with
corresponding names in the phone directory.
For Example:

Creating a Dictionary:
Using a flower bracket or dict() function dictionary is created. Working with a dictionary, needs
key-value pairs to be added.
For Example:
1.my_dict = {} #empty dictionary
Output:{}
2.my_dict = {1: ‘Python‘, 2: ‘Java’} #dictionary with elements
Output:{1: ‘Python’, 2: ‘Java’}

Image Source: Data Flair

Adding Key-Value in Dictionary:
This is achieved by simply adding a key and its value.
For Example:
1.my_dict = {1: ‘Python’, 2: ‘Java’}
Output:{1: ‘Python’, 2: ‘Java’}
2.my_dict[3]=’Ruby’ #Added key 3 and value Ruby.
Output:{1: ‘Python’, 2: ‘Java’, 3:’Ruby’}
3.my_dict[‘learn’’]=’Grails’
Output:{1: ‘Python’, 2: ‘Java’, 3:’Ruby’,’learn’:’Grails’}
Deleting Key-Value in Dictionary:
Deleting Key-value in Dictionary can be achieved through
1.pop()
2.popitem()
For Example:
1.my_dict = {1: ‘Python’, 2: ‘Java’, 3:’Ruby’}
Output:{1: ‘Python’, 2: ‘Java’, 3:’Ruby’}
2.a = my_dict.pop(3) #pop element
Output:Ruby

3.b = my_dict.popitem() #pop the key-value pair
Output:2: ‘Java’
Important Other Functions in Dictionary:
1.keys() Returns the list of Keys
2.values() Returns the list of values
3.get() Returns the value of specified Key
4.items() Returns tuple of each key-value pair
5.copy() Returns a copy of the dictionary.
For example:
1.my_dict.keys() #Get all the keys
Output: [1,2,3]
2.my_dict.values() #Get all the values
Output:[‘Python’,’Java’,’Ruby’]

Tuples: It is very similar to List with the difference of mutability. Once data is entered into tuples,it
can not change, making tuple immutable. The size of the tuples is fixed.

For Example:
Creating a Tuple:
1.my_tuple = (1, 2, 3) #create tuple
Output: (1,2,3)

Accessing Elements in Tuples
2.print(my_tuple[0])
Output: 1
3..print(my_tuple[:])
Output: (1,2,3)

Other Important Functions in Tuples
1.tuple() Convert list into tuple.
2.min() Returns the smallest value from a tuple
3.max() Returns the largest value from a tuple
4.cmp() Compares elements of two tuples
5.len() Returns total length of tuple

For Example:

  • my_tuple = (1, 2, 3, [‘hindi’, ‘python’])
    print(my_tuple[3][0])
    Output: hindi
  • my_tuple.count(2)
    Output: 1
  • my_tuple.index([‘hindi’, ‘python’])
    Output: 3

Difference between List And Tuple?
This is an interesting topic that usually asked everywhere. Let’s discuss some points clarifying this difference.

  1. Lists are mutable whereas tuples are immutable.
  2. 2.Syntax Difference:
    List: [1, 2, 3, 4]
    Tuple: (1, 2, 3, 4)
  3. The list has variable length while tuples have fixed length.
  4. The list has usually more functionality than Tuples.

Sets: It basically is a collection of unordered elements that are unique. By unique, we mean that
whether there is repeated data but in the set, no data will be repeated, this is the most important
property of sets. Sets usually have similar operations as we have in algebra.

For Example:
Creating a Set:
We don’t pass the key-value pair instead only values are passed in the set.
1.my_set={1, 2, 2, 2, 3, 3, 5, 5} #creating set
Output:{1, 2, 3, 5}

Adding Elements in Set:
1.my_set={1,2,3,4,4,4,5}
Output:{1, 2,3, 4, 5}
2.my_set.add(6); #adding element in set
Output:{1, 2, 3, 4, 5, 6}
Different Operations in Set:
For Example:
my_set_1={1,2,3,4}
my_set_2={3,4,5,6}
1.Intersection
my_set_1.intersection(my_set_2);
Output:{3,4}
2.Union
my_set_1.union(my_set_2);
Output:{1, 2, 3, 4, 5, 6}
3.Difference
my_set_1.difference(my_set_2);
Output:{1, 2}

Conclusion:
1.Order:
List and Tuple are ordered while Dictionary and Set are unordered.
2.Mutable:
Except for Tuple, the other three are mutable.
3.Constructor:
List:[]
Tuple:()
Set:{}
Dictionary:{}

Different Users of Inbuilt Data Structure:

  1. Tuples:
    a)To provide easy access to a dataSet and easy manipulation of a data set.
    b)Through a single Parameter passing multiple values.
  2. Dictionary:
    a)Used to store unordered key-value pairs.
    b)Anything can be used to store the data and to access them basically.
  3. List:
    a)Multiple data types are stored and multiple actions can be performed too.
    b)Easy access and manipulation of data sets.
  4. Set:
    a)To store unique data basically.

To read more about Python, click here.

By Deepak Jain