Must learn data structures while you start your Python Learning
Inbuilt Data Structures:
Inbuilt Data structures in python are
- List []
- Tuple ()
- Dictionary {}
- Set

These we can call as non -primitive types in python because these methods can be used to call methods to perform certain operations. List,Tuple,Dictionary are the basic linear data structure of python.Lets see one by one.
List
A List in python data structure is a changeable, ordered sequence of elements. Each element or value inside a list is known as an Item.Each item in a list separated by a comma and enclosed within square brackets[].
##List of Fruits
fruits =['Apple','Orange','Jack Fruit']
fruits
Output:
['Apple', 'Orange', 'Jack Fruit']
A list can have any number of items and they may be of different types (integer, float, string, etc.).
##List with mixed data types
L2=[‘A’,’B’,’c’,1,2,3]
L2
Output:
['A', 'B', 'c', 1, 2, 3]
Nested List
A list can also have another list as an item.This is called nested list.
##Nested List
L3 =["Element", [1,2,3], ['Z']]
L3
Output
['Element', [1, 2, 3], ['Z']]
List Index is one of the method to access an item from the list.
In Python, indices start at 0. So, a list having 7elements will have an index from 0 to 6.
##List Indexing
## This list have an index from 0 to 3
List=[‘Apple’,’Banana’,’Orange’,’kivi’]
print(List[2])
## Negative indexing
print(List[-1])Output
Orange
kivi
Nested Lists are accessed using Nested Indexing
List1=['Hello',[1,2],'M']
print(List1[0][1])
print(List1[1][1])
print(List1[2])Output
e
2
M
Create an empty List
## Create an empty list
Movies=[]
Movies
Output:
[]
Add an item to the List
Movies=[‘Luca’,’Soul’,’up’]
print(Movies)
##Add an item to the List
Movies.append(‘Nemo’)
print(Movies)
Animation_movies=[‘Toothless Dragon’,’Turning Red’,’Rio’,’Toystory’]
Animation_movies.append(Movies)
print(Animation_movies)
Output:
[‘Luca’, ‘Soul’, ‘up’]
[‘Luca’, ‘Soul’, ‘up’, ‘Nemo’]
[‘Toothless Dragon’, ‘Turning Red’, ‘Rio’, ‘Toystory’, [‘Luca’, ‘Soul’, ‘up’, ‘Nemo’]]
Remove an item from the List:
## Remove an item from the list
Animation_movies.remove(‘Turning Red’)
print(Animation_movies)
Output
['Toothless Dragon', 'Rio', 'Toystory', ['Luca', 'Soul', 'up', 'Nemo']]
Total items in the List
###Total Items in the List
print(len(Animation_movies))Output:
4
Clear all items in the List
##Clear all items in the List
List=[‘Apple’,’Banana’,’Orange’,’kivi’]
List.clear()
print(List)Output:
[]
Using a for loop we can iterate through each item in a List.
colours=[‘White’,’Red’,’Yellow’,’pink’]
for colour in colours:
print(“I like “,colour)
Output:
I like White
I like Red
I like Yellow
I like pink
Tuple()
Tuple is a collection of immutable objects in python.Objects can be of any data types.A tuple is created by placing all the items inside parentheses ()
, separated by commas.
Tuple=() ## Empty tuple
T1=(‘C’,’C++’,’Java’)
print(T1)
T2=(1,’Hai’,2,3.1)
print(T2)
t3 = (“nested”, [7,8,9], (3,2,1))
print(t3)##Call Tuple Value Using Indexprint(T1[2])##Find the Index of Tuple Valueprint(T1.index('Java'))
Output:
('C', 'C++', 'Java')
(1, 'Hai', 2, 3.1)
('nested', [7, 8, 9], (3, 2, 1))
Java
2
Tuple allow duplicate values and How to count the presence of value in the Tuple.
## Tuple allow duplicate values
student=(‘Ram’,’Sam’,’Vino’,’Meena’,’Ram’)## Count the presence of value in typlestudent.count(‘Ram’)
Output:
2
Tuples are the same as lists are with the exception that the data once entered into the tuple cannot be changed.
tup=(1,2,3,’Hai’,’I am tuple’)
print(tup)
##Accessing element from tuple
tup
for t in tup:
print(t)
print(tup[1])
print(tup[:])
print(tup[3][2])
Output:
(1, 2, 3, 'Hai', 'I am tuple')
1
2
3
Hai
I am tuple
2
(1, 2, 3, 'Hai', 'I am tuple')
i
Dictionary {}
Dictionary: Collection of information about one item.Dictionary are used to store key value pair.
Dictionaries can be created using {},We need to add the key-value pairs whenever you work with dictionaries.
## Empty Dictionary
mydic={}
print(mydic)Output:
{}
Creating Dictionary with Keys
mydic={1:’List’,2:’Tuple’,3:’Dictionary’}
print(mydic)
Output:
{1: 'List', 2: 'Tuple', 3: 'Dictionary'}
Changing element and adding key:Value pair in Dictionary
##changing element
mydic[2]=’Zip’
print(mydic)##Adding key value pair
mydic[4]=’Tuple’
print(mydic)
mydic[3]=’Dictionary’
print(mydic)
Output:
{1: 'List', 2: 'Zip', 3: 'Dictionary'}
{1: 'List', 2: 'Zip', 3: 'Dictionary', 4: 'Tuple'}
{1: 'List', 2: 'Zip', 3: 'Dictionary', 4: 'Tuple'}
Dictionary other functions
print(mydic)
print(mydic.values())
print(mydic.keys())
print(mydic.items())
print(mydic.get(2))
Output:
{1: 'List', 2: 'Zip', 3: 'Dictionary', 4: 'Tuple'}
dict_values(['List', 'Zip', 'Dictionary', 'Tuple'])
dict_keys([1, 2, 3, 4])
dict_items([(1, 'List'), (2, 'Zip'), (3, 'Dictionary'), (4, 'Tuple')])
Zip
Set
A set is a collection of items which is unordered, un-indexed, and unchangeable but we can add or remove items to the set.Duplicates are not allowed in sets.
Sets are written with {} brackets.
##Creating Python Sets
s={‘C’,’C++’,’Java’}
print(s)
s1={1,2,3,’Hai’,4,5,6.5}## Sets of mixed data types
s1
Output:
{'C', 'C++', 'Java'}{1, 2, 3, 4, 5, 6.5, 'Hai'}
Set cannot have duplicates
### set cannot have duplicatess={1,2,3,4,1,2,3}
print(s)
Output:
{1, 2, 3, 4}
Adding item to the set
s1={2,4,6}
print(s1)## Adding item to the set
s1.add(8)
print(s1)
Output
{2, 4, 6}
{8, 2, 4, 6}
How to add multiple items and remove an item in the set
### adding multiple elementss1.update([1,3,5,7])
print(s1)
# remove an element
s1.remove(3)
s1
Output:
{1, 2, 3, 4, 5, 6, 7, 8}{1, 2, 4, 5, 6, 7, 8}
I hope you have gained a solid understanding of the fundamentals of
Inbuilt Data structures in python.