• By
• May 11, 2022
• Python

Data Structure in Python  –

Python has been used worldwide for different fields such as making websites, artificial Intelligence, Data analysis, Machine learning and much more. But to make all of this possible, data plays a very important role which means that this data should be stored efficiently and the access to it must be timely.

What is Data structure?

Organizing, managing and storing data is important as it enables easier access and efficient modifications. Data Structures allows you to organize your data in such a way that enables you to store collections of data, relate them and perform operations on them accordingly.

Types of Data structure in Python –

1. Built-in-Data structure
2. In Built Data Structure

Built-in-Data structure-

1. List
2. Tuple
3. Dictionary
4. Set

List :

• List is collection of multiple values stored in single variable
• List is ordered, Allows duplictes in list
• List is mutable, you can store the various datatypes in list
• List is created by [  ]

Below are the empty list

l1 = [ ]

Method of list

append method – store the single value in end of the list

l1.append(10)

print(l1)

Output- [10]

• Extend Method

l1.extend((22,33,44,55,66)) #Extend method is used to store multiple values at a time and result is store in end of the list

print(l1)

output-[10, 22, 33, 44, 55, 66]

l1.extend((100,200,300,400,5,10,15,25,35,65,75,165))

print(l1)

[10, 22, 33, 44, 55, 66, 100, 200, 300, 400, 5, 10, 15, 25, 35, 65, 75, 165]

• Insert method

l1.insert(1,2000) #Insert method is  store the value by specific position

print(l1)

[10, 2000, 22, 33, 44, 55, 66, 100, 200, 300, 400, 5, 10, 15, 25, 35, 65, 75, 165]

Deleting method from the list

Pop –

l1.pop(3) #pop method is used to remove the specified indixing

print(l1)

[10, 2000, 22, 44, 55, 66, 100, 200, 300, 400, 5, 10, 15, 25, 35, 65, 75, 165]

Value 33 remove from the list

Remove –

• l1.remove(2000) # remove method is used to remove the given value
• print(l1)
• [10, 22, 44, 55, 66, 100, 200, 300, 400, 5, 10, 15, 25, 35, 65, 75, 165]
• 2000 value is removed from the list

Del –

del l1[4] # del method  is used to remove the value by specified indexing

print(l1)

[10, 22, 44, 55, 100, 200, 300, 400, 5, 10, 15, 25, 35, 65, 75, 165]

66 value is deleted from the list

Clear –

l1.clear()

print(l1) # clear method is used to clear the entire list and  give the empty list

[]

Count –

print(l1.count(55)) #count method display the specified value how many times comes in list

1

Index –

print(l1.index(200))# index method shows the indexing value of specified value

Sort –

print(“Before sorting”,l1)

l1.sort() # sort the list in  asscending order and returns none

print(“After sorting”,l1)

Before sorting [10, 22, 44, 55, 100, 200, 300, 400, 5, 10, 15, 25, 35, 65, 75, 165]

After sorting [5, 10, 10, 15, 22, 25, 35, 44, 55, 65, 75, 100, 165, 200, 300, 400]

reverse –

l1.reverse() # reverse the list

print(l1)

[400, 300, 200, 165, 100, 75, 65, 55, 44, 35, 25, 22, 15, 10, 10, 5]