Data Structures in Python
Let us know about the data structures in python. There are four basic built-in data structures in python namely list, tuple, dictionary and set. Lets see everything one by one.
List:
List is a mutable array. It supports different data types. Lists are used to store multiple items in a single variable. It allows duplicate values. List items are indexed where LHS indices from 0 and RHS indices from -1. If you add a new item to the list, it will be placed at the end of the list. List have some items like append, pop, remove, extend etc.. For example ,lets take append here
Example:
a=[1,2,3,4,5]
a. append(10)
then,
a=[1,2,3,4,5,10], see here, in append the new item will be added as a last item in the list. But, append can take one argument at a time.
Pop,
a=[1,2,3,4,5]
a. pop()
then,
a=[1,2,3,4] , here in pop the last item of list is popped that means deleted. It can also remove the element by index value also.
Remove,
a=[1,2,3,4,5]
a. remove(5)
then,
a=[1,2,3,4] , here in remove the mentioned element in the list will be removed.
Extend,
a=[1,2,3,4,5]
a. extend([6,7])
then,
a=[1,2,3,4,5,6,7], by extend we will add multiple arguments at a time.
Dictionary:
It is a mutable array. Keys cannot be duplicate. Dictionaries are used to store data. Dictionary is a collection which is ordered. It is written with curly braces and have keys and values.
Dictionary methods:
keys()-Returns a list containing the dictionary’s keys.
Example: my_dict={‘age’:22, ‘city’ : ‘Chennai’, ‘name’ : ‘Santhosh’}
my_dict.keys()
output: dict_keys([‘name’, ’age’, ’city’])
value()-Returns a list of all the values in the dictionary.
Example: my_dict={‘age’:22, ‘city’ : ‘Chennai’, ‘name’ : ‘Santhosh’}
my_dict.value()
output: dict_values([‘Santhosh’,22,’Chennai’])
item()-Returns a list containing a tuple for each key value pair.
Example: my_dict={‘age’:22, ‘city’ : ‘Chennai’, ‘name’ : ‘Santhosh’}
my_ dict.items()
output: dict_ items([(‘name’, ‘Santhosh’),(‘age’, ‘22’),(‘city’, ‘Chennai’)]).
TUPLE:
Tuple is immutable array. It supports different data types .It is used to store multiple items in a single variable. List and tuple are same, tuple has indexing and slicing. It is called as typecasting.
1.INDEXING:
Example1:
my_ tuple=(1,2.5,’hello’,3232)
my_ tuple[0]
Output:1
Example2:
Get Santhosh J’s stories in your inbox
Join Medium for free to get updates from this writer.
my_ tuple=(1,2.5,’hello’,3232)
my_ tuple[-1]
Output:3232
2.SLICING:
my_ tuple=(1,2.5,’hello’,3232)my_ tuple[0:2]
Output:[1,2.5]
TUPLE METHODS:
Methods that add items or remove items are not available with tuple. Only the following two methods are available. They are,
1.Count
2.Index
Example,
my_ tuple=(‘a’, ‘p’ , ‘p’ , ‘l’ ,’ e’)
print(my_ tuple. count(‘p’))
print(my_ tuple. index(‘l’))
Output:
2
3
Set:
Set is also a mutable array. Set are used to store multiple items in a single variable. Set doesn’t support indexing. Set contains only unique values. It support all data types.
Set removes the duplicate values.
Example:
a={1,2,3,’pyhton’,7,2,3}
set. type(a)
a={1,2,’python’,7}, see here it removes the duplicate values.







