Data structure, way in which data are stored for efficient search and retrieval. Different data structures are suited for different problems.
4 basic types of Data Structure:
- List
- Dictionary
- Tuple
- Set
List & Tuple:
List and Tuple objects are sequences. They are ordered collection of items. List and tuple items are indexed.
Important Note:
list is mutable array.
strings are immutable array.
Types:
append
extend
insert
remove
pop
clear
index
count
sort
reverse
copy
Creating a list/tuple:
list = []
tuple = ( )
Example: length calculation
list:
a=[1,2,3,4,5]
tuple:
a=(1,2,3,4,5)
len(a)
5
Append:
The append( ) method appends an element to the end of the list.
Append can take only one argument at a time.
list:
syntax : list.append(element)
tuples are immutable!
Pop:
The pop() method removes the element at the specified position.
list:
b=[1 , 2.5 , ‘hi’ , 5432]
b.pop( ) #removes the last element
b.pop(1) #removes the element in mentioned index
print(b)
[‘1’ , ‘hi’]
tuple:
tuples are immutable!
Insert:
The insert() method inserts the specified value at the specified position.
Insertion takes both position and value.
list:
a=[1 , 2 , 3 , 4]
a.insert(2,1000)
print(a)
a=[1 , 2 , 1000 , 3 , 4]
tuples:
tuples are immutable.
Count:
The count( ) method returns the number of times occurrence of the specified element.
list:
c=[3 , 5 , 7 , 5 , 2]
c.count(5)
2
tuple:
tuples are immutable
Extend:
Multiple values added at a time.
list:
c=[1 , 2 , 3 , 4]
c.extend([15,30,20])
print(c)
c=[1,2,3,4,5,15,30,20]
nested list:
c.append([100,200, ‘hi’])
print(c)
c=[1,2,3,4,15,30,20,[100,200, ‘hi’]]
Concatenate two lists/tuples:
Lists: myList1 + myList2
Tuples: myTuple1 + myTuple2
Concatenating a List and a Tuple will produce a TypeError exception.
Dictionaries:
It is an unordered set of key-value pairs. In python you can only access the keys of the dictionaries, one of the main features of the dictionary is its a fast and efficient way to store data.
Initialize an empty Dictionary:
d = {}
Add an element with key “name” to the Dictionary:
d["name"] = 'jon'
{'name':'jon'}
Update the element with key “name”:
Syntax:
d["name"] = 'tom'
d['age'] = 25
{'name':'tom','age':25}
Get element with key “k”:
d["name"]
'tom' -- If the key is not present, a KeyError is raised
Check if the dictionary has key “k”:
"name" in d
True
Get the list of keys:
d.keys()
['name','age']
Get the size of the Dictionary:
len(d)
2
Delete element with key “k” from the Dictionary:
del d["name"]
{'age':25}
Delete all the elements in the Dictionary:
d.clear()
{}
Sets:
A set is a sequence of objects which is unordered and not indexed. Their values are also unchangeable.
Creating Sets:
mySet = {“Pavi”, “Berlin”, “Imran”, “Charles”, “Priya”}
print(mySet)
{‘Pavi’, ‘Berlin’, ‘Imran’, ‘Charles’, ‘Priya’}
Note: Whenever you print sets, the order of the elements will differ as they are unordered.
Deleting Sets and Set Elements:
del mySet
remove function:
mySet= {‘Pavi’, ‘Berlin’, ‘Imran’, ‘Charles’, ‘Priya’}
mySet.remove(“Imran”)
print(mySet){‘Pavi’, ‘Berlin’, ‘Charles’, ‘Priya’}
FrozenSet( ):
The frozenset() function returns an unchangeable frozenset object (which is like a set
object, only unchangeable)
mylist = [‘apple’, ‘banana’, ‘cherry’]
x = frozenset(mylist)
print(x)
frozenset({‘banana’, ‘cherry’, ‘apple’})