Data Structures in
Python
List
Declaring List OR Creating List OR
Defining List
• A List can be declared by placing all elements inside ( [ ] ) separated
by (,). We can either declare an empty list and append it later.
• List1 = [ ]
• List2 = [10,20,30]
• List3 = [10, “Ravi”, 3.11,1456864]
• List4 = [176,399,76.96,[60,70.80],(100,200,300),List2]
Accessing list Elements using Index
Numbers
List1 = [10,20,30,40,50]
print(list1[0])
print(list1[1])
print(list1[2])
print(list1[3])
print(list1[4])
Negative list Indexing
List1 = [10,25,30.55,“Raj”]
print(List1[-1])
print(List1[-2])
print(List1[-3])
Iterating through list using for loop
List1 = [10,20,30,40,50]
sum = 0
for ele in List1:
sum = sum + ele
Print(sum)
Slicing Lists(using Slicing
Operator[:])
list1 = [‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’, ‘M’, ‘I’, ‘N’, ‘G’]
print(list1)
list2 = list1[5:]
print(list2)
list3 = list1[:-5]
print(list3)
list4 = list1[::-1]
print(list4)
Output
[‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’, ‘M’, ‘I’, ‘N’, ‘G’]
[‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’, ‘M’, ‘I’, ‘N’, ‘G’]
[‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’]
[‘G’, ‘N’, ‘I’, ‘M’, ‘M’, ‘A’, ‘R’, ‘G’, ‘O’, ‘R’, ‘P’, ‘N’, ‘O’, ‘H’, ‘T’, ‘Y’, ‘P’]
Basic List Operations
• Adding element : append(), insert() and extend()
Functions
• append(): -
append adds specified element to the last position of list.
• Syntax :- list_name.append(New_element)
Example: -
list1 = [10,20, “abc”,23.678,1234]
print(list1)
list1.append(520)
print(list1)
list1.append(“xyz”)
print(list1)
list1.append(30)
print(list1)
Insert():- inserts an element at specified position.
Syntax: - list_name.insert(position/index, element)
Example: -
list1 = [“Maths”, “Physics”, “Chemistry”, “Biology”, “English”]
print(list1)
list1.insert(2, “Sanskrit”)
print(list1)
list1.insert(4, “History”)
print(list1)
extend():- Adds elements of parameter List with invoking List.
Syntax: - list1.extends(list2)
Example: -
list1 = [10,20,30]
list2 = [40,50]
list1.extend(list2)
print(list1)
Finding Length of List : using __len__()
Function
list1 = [76.89,50,764, “abc”, “xyz”, 74397]
a = list1.__len__()
print(“Length of list is: ”,a)
Delete or Remove Element from list
1.del: keyword is used to remove one or more elements from list.
list1 = [‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’, ‘M’, ‘I’, ‘N’, ‘G’]
print(list1)
del list1[0]
print(list1)
del list1[5]
print(list1)
del list1[1:3]
print(list1)
2. remove(): This function removes specified item from list.
Syntax: list_name.remove(element)
3. pop(): This function is used to remove element from specified
index.
Syntax: list_name.pop(index_number)
4. clear(): This function removes all elements from a list.
Syntax: list_name.clear()
list1 = [‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’]
print(list1)
list1.remove(‘T’)
print(list1)
list1.pop(5)
print(list1)
list1.pop(8)
print(list1)
list1.clear()
print(list1)
Nested list
a list is an element of another list.
List1 = [10,20,[30,40,50], 60,[“abc”,78.90,67],65.53, “xyz”, 86]
Built-in List functions
append() append(element)
extend() extend(sequence)
insert() insert(index,element)
remove() remove(element)
pop() pop(index)
clear() clear()
index() index(element_to_search)
count() count(element)
sort() sort()
reverse() reverse()
copy() copy()
Deleting List
list = [10, 20, 50.60, “abc”, 50]
print(list)
del list
print(list)
Tuples
• Declaring Tuple
tuple1=(10,20,30,581.65, “abc”, 45.368)
tuple2=(2.55,45.36)
tuple3=(“abc”, “mno”, “xyz”)
tuple4=(100,[10,20,30],588,(5.36,96.578, “abc”),520)
Accessing tuples
tuple1=(10,20,30,581.65, “abc”, 45.368, 15.71, 96357,
“xyz”, 857.36985)
print(tuple[0])
print(tuple[2:4])
print(tuple[3: ])
print(tuple[::-1])
Negative Tuple indexing
tuple1=(10,20,30,581.65, “abc”, 45.368, 15.71, 96357, “xyz”,
857.36985)
print(tuple[-1])
print(tuple[-3])
print(tuple[-5])
print(tuple[::-1])
Iterating through tuple using
for-loop
tuple=(10,20,40,5.368,52.84, “abc”)
for ele in tuple:
print(ele)
Slicing tuple
tuple1 = [‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’, ‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’]
print(tuple1)
tuple2 = tuple1[3:8]
print(tuple2)
tuple3 = tuple1[ :5]
print(tuple3)
tuple4 = tuple1[ :8]
print(tuple4)
Basic tuple operations
Tuples are immutable
tuple1 = (10,20,53.689, “abc”)
tuple2 = tuple1 + (50,60)
print(tuple2)
Finding length of tuple using
__len__()
tuple = (10,20, “abc”, 63.80)
a = tuple.__len__()
print(“Length of tuple is: ”,a)
Testing Membership Operators on Tuple
tuple = (10, 20, “Riya”, 50.658, 40.80, 100 )
print(“presence of 10 in tuple”, 10 in tuple)
print(“presence of 50 in tuple”, 50 in tuple)
print(“presence of 40.80 in tuple”, 40.80 in tuple)
print(“presence of 80 not in tuple”, 80 not in tuple)
print(“presence of 10 not in tuple”, 10 not in tuple)
print(“presence of 600 not in tuple”, 600 not in tuple)
print(“presence of Riya in tuple”, “Riya” not in tuple)
Concatenation in tuple
tuple1=(10,20,30,581.65, “abc”, 45.368)
tuple2=(2.55,45.36)
tuple3 = tuple1 + tuple2
print(tuple3)
Multiplying Tuple
tuple = (10, 50.63, “abc”, 65.90, 500)
tuple1 = tuple * 2
print(tuple)
print(tuple1)
Built-in tuple functions
count() count(element)
index() index(element)
__add__() __add__(tuple)
tuple1 = (30, 10, 20, 30)
print(tuple1)
tuple2 = tuple1.__add__((50, 80.90, 56.324))
print(tuple2)
a = tuple1.count(30)
print(“ appearance of 30 in tuple1: ”,a)
b = tuple1.index(30)
print(“index position of 30 in tuple1 is: ”,b)
Deleting tuple
tuple = (10, 20, “abc”, 86.50)
print(tuple)
del tuple
print(tuple)
Set
• Sets are unordered.
• Set elements are unique. Duplicate elements are not
allowed.
• A set itself may be modified, but the elements contained in
the set must be of an immutable type.
Declaring set
set1 = {10, 20, 30}
print(set1)
set2 = {1.45, ‘hello’, (1,2,3) }
print(set2)
No indexing to set elements
set1 = {10,20,30}
print(set1[1])
#It will show error
For-loop
set1 = set({10,20,30,40})
for i in set1:
print(i)
Basic set Operation
• update():- update function can take Tuples, Lists, Strings or
other Sets as its parameter.
• add():- we can add single element using add() function.
set1 = {1,3}
print (set1)
set1.add(2)
print (set1)
set1.update([2,3,4])
print (set1)
set1.add(10)
print (set1)
Finding length using len() function
set1 = {10, 20, 50.365, “hello”, 53.588}
a = set1.__len__()
print(set1)
Removing Element :-
• discard()
• remove()
• pop()
• clear()
set1 = {1,2,3,4,5,6,7,8}
print(set1)
set1.discard(2)
print(set1)
set1.remove(8)
print(set1)
set1.pop()
print(set1)
set1.clear()
print(set1)
Set Union
A = {1,2,3,4,5}
B = {4,5,6,7,8}
C = A | B OR C = A.union(B)
print(A)
print(B)
print(C)
Set intersection
A = {1,2,3,4,5}
B = {4,5,6,7,8}
C = A & B OR C = A.intersection(B)
print(A)
print(B)
print(C)
Set difference
A = {1,2,3,4,5}
B = {4,5,6,7,8}
C = A - B OR C = A.difference(B)
D = B – A OR D = B.difference(A)
print(A)
print(B)
print(C)
print(D)
Set Symmetric_Difference
A = {1,2,3,4,5}
B = {4,5,6,7,8}
C = A ^ B OR C = A. symmetric_difference(B)
print(A)
print(B)
print(C)
Additional Set Functions
• add() • isdisjoint()
• clear() • issubset()
• copy() • isuperset()
• difference() • pop()
• difference_update()
• remove()
• symmetric_difference()
• discard()
• symmetric_difference_update()
• intersection
• union()
• intersection_update
• update()
Deleting set
set1 = {10,20,30}
print(set1)
del set1
print(set1)
Dictionaries
• Creating Dictionary
dict = {}
dict1 = {1: “Apple”, 2: “Ball”}
dict2 = {“name”: “abc”, 1: [2,3,4]}
dict4 = { [ (1, “Apple”), (2, “Ball”) ] }
Accessing Dictionary
dict1 = { “Rina”: “14/02/2015”, “Tina”: “02/05/1985”,
“Kunal”: “18/04/1879”}
print(dict1[“Rina]”)
print(dict1[“Kunal”])
Update() Function
dict1 = { “Rina”: “14/02/2015”, “Tina”: “02/05/1985”, “Kunal”:
“18/04/1879”}
dict1.update({“Jahnvi”: “09/08/1987”})
print(dict1)
dict1.update({“Tina”: “09/08/1985”})
print(dict1)