Show Menu
Cheatography

datastructures in python Cheat Sheet (DRAFT) by

comparison between the different data structures in python

This is a draft cheat sheet. It is a work in progress and is not finished yet.

LIST

List is a data structure which can hold multiple values
It can be defined using [] or list()
`lst = [1,2,3,4]
lst2 = list((­1,2­,3,4))'
It can be a collection of different data types
list1 = [1,"­hi",­2.5­,False]
It can be accessed by indexing and is ordered
print(­lis­t1[2]) ---->2.5
We can modify the values of the list using indexing- mutable
list1[­3]=­"­hel­lo"-­---­-->­[1,­"­hi",­2.5­,"He­llo­"]
 

TUPLE

Tuple is a data structure which can hold multiple values
It can be defined using () or list()
`tup= [1,2,3,4]
tup2= tuple(­(1,­2,3­,4))'
It can be a collection of different data types
tup2= (1,"­hi",­2.5­,False)
It can be accessed by indexing and is ordered
print(­tup­2[2]) ---->2.5
We cannot modify the values of the tuple using indexing- immutable
tup2[3­]="h­ell­o"--­---­->error
 

SETS

Set is a data structure which can hold multiple values
It can be defined using {} or set()
`set1= {1,2,3,4}
set2 = set((1­,2,­3,4))'
It can be a collection of different data types
set1= {1,"­hi",­2.5­,False}
Itcannot be accessed by indexing and is unordered
print(­set­2[2]) ---->error
We can modify the values of the list using iteration- mutable
 

dictionary

Dictionary is a data structure which can hold multiple values
The values are stored in a key value pair
It can be defined using () or dict()
`dict1= ("Name":"Tom","age":23)
lst2 = list((­1,2­,3,4))'
It can be a collection of different data types
list1 = [1,"­hi",­2.5­,False]
It can be accessed by indexing and is in order
print(­lis­t1[2]) ---->2.5
We can modify the values of the list using indexing- mutable
list1[­3]=­"­hel­lo"-­---­-->­[1,­"­hi",­2.5­,"He­llo­"]