Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

s-or-data-structures-or-containers

February 27, 2023

1 Advanced Data Types or Data Structures or Containers


1.1 List
• it is the most used data type in python and is very flexible
• Empty List
• to create an empty list we can use the empty square brackets ‘[]’ or the ‘list()’ function with
no arguments
[1]: l = []
l

[1]: []

[2]: type(l)

[2]: list

[3]: l1 = list()
l1

[3]: []

[4]: type(l1)

[4]: list

• list is an ordered sequesnce of items


• Each element or a value that is present inside a list is called an item
• declaring a list - items saperated by commas that are enclosed in a square brackets.
[5]: l1 = [1,2,3,4]
print(l1)

[1, 2, 3, 4]

[6]: l1

1
[6]: [1, 2, 3, 4]

• All items in a list do not need to be a particular data type so lists are heterogenous.
[8]: l2 = [1,2.0, 3+4j, True, 'krishna']
l2

[8]: [1, 2.0, (3+4j), True, 'krishna']

• Length of the list


[9]: m = [1,20.5, 'hello']
len(m)

[9]: 3

• Nested List
[10]: nest = [1,2,[3,4],5]
nest

[10]: [1, 2, [3, 4], 5]

[11]: len(nest)

[11]: 4

• List indexing
[12]: nest[0]

[12]: 1

[13]: nest[1]

[13]: 2

[17]: nest[1][0]

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [17], in <cell line: 1>()
----> 1 nest[1][0]

TypeError: 'int' object is not subscriptable

[18]: nest[2][1]

2
[18]: 4

[19]: nest[2][0]

[19]: 3

[22]: a = [1,2,[4,5,[8,'hello',6]],45,99]
a

[22]: [1, 2, [4, 5, [8, 'hello', 6]], 45, 99]

[24]: a[2][2][1][3]

[24]: 'l'

[25]: a[1]

[25]: 2

• Lists are mutable


• Value of an elements of a list can be altered in the same object
[26]: a = [1,2,3]
print(a)

id(a)

[1, 2, 3]

[26]: 2265537079616

[28]: a[1] = 20
print(a)
id(a)

[1, 20, 3]

[28]: 2265537079616

• List Slicing
[31]: num = [0,10,20,30,40,50,60,70,80,90]
num[0:5]

[31]: [0, 10, 20, 30, 40]

• List Concatenation

3
[1]: l1 = ['dsad','b','c']
l2 = ['a',6,4.0]
l3 = l1+l2
l3

[1]: ['dsad', 'b', 'c', 'a', 6, 4.0]

• List methods
• list.append() - append is used to add the elements in the list and it will add at the end of the
list only one item is added at a time in appned
[7]: mylist = [1,2,3,4]
print(mylist)
mylist.append(5)
mylist

[1, 2, 3, 4]

[7]: [1, 2, 3, 4, 5]

[8]: mylist1 = [1,2,3,4]


print(mylist1)
mylist1.append([5,6])
mylist1

[1, 2, 3, 4]

[8]: [1, 2, 3, 4, [5, 6]]

• list.extend() - it adds the items in the list induvidually at the end but we can add multiple
items at a time.
• in order to use extend the elements needed to provided in list only
[18]: mylist2 = [1,2,3,4]
print(mylist2)
mylist2.extend([6,7])
mylist2

[1, 2, 3, 4]

[18]: [1, 2, 3, 4, 6, 7]

[19]: ml = [1,2,3,4,5]
ml.extend([6])
ml

[19]: [1, 2, 3, 4, 5, 6]

4
[20]: mylist3 = [1,2,3,4,5,6]
mylist3.extend([1,[4,5],8,9,[10,33]])
mylist3

[20]: [1, 2, 3, 4, 5, 6, 1, [4, 5], 8, 9, [10, 33]]

• list.insert() - to insert in a specific order and list.insert(x,y) will add item y at location x

[1]: lst = ['one','two','three']


lst.insert(2,'four')
lst

[1]: ['one', 'two', 'four', 'three']

• list.remove() - remove an item based on a value

[2]: lst1 = [1,2,3,4,5]


lst1.remove(5)

[3]: lst1

[3]: [1, 2, 3, 4]

[4]: lst1.remove(3)

[5]: lst1

[5]: [1, 2, 4]

• list.pop() - remove an item based on index number

[15]: l1 = [1,2,3,4,5,6,7,8,9]
l1.pop(2)
print(l1)

[1, 2, 4, 5, 6, 7, 8, 9]

[16]: l1

[16]: [1, 2, 4, 5, 6, 7, 8, 9]

[17]: l1.pop(1)
print(l1)

[1, 4, 5, 6, 7, 8, 9]

[18]: l1.pop(3)
print(l1)

5
[1, 4, 5, 7, 8, 9]

[19]: l1.pop(2)
print(l1)

[1, 4, 7, 8, 9]

[22]: l2 = [10,20,30,40,50,60,70,80,90]
del l2[0:4]

[23]: l2

[23]: [50, 60, 70, 80, 90]

• list.clear() - used to clear all the items and empties the list

[27]: l3 = [1,2,3,4,5,[3,5,[4,6,[77,44],56],66],49]
l3

[27]: [1, 2, 3, 4, 5, [3, 5, [4, 6, [77, 44], 56], 66], 49]

[28]: l3.clear()
print(l3)

[]

[30]: l4 = [1,2,3,4,5]
l4.clear()
l4.append(6)
print(l4)
l4.extend([7,8,9,0])
print(l4)

[6]
[6, 7, 8, 9, 0]
• list.reverse() - it is used to reverse the entire list

[31]: ls1 = [1,2,[3,4],5]


ls1.reverse()
print(ls1)

[5, [3, 4], 2, 1]

[34]: ls1[1].reverse()

[35]: ls1

[35]: [5, [4, 3], 2, 1]

6
• list.sort() - it is used to sort the list in ascending order or the descending order

[36]: ls2 = [1,2,3,4,5,6]


ls2.sort()

[37]: print(ls2)

[1, 2, 3, 4, 5, 6]

[38]: ls2.sort(reverse=True)
print(ls2)

[6, 5, 4, 3, 2, 1]

[40]: l = ['a','c','s','b','d','f','r','e','g','k','sad','das','kim']
l.sort()
print(l)

['a', 'b', 'c', 'd', 'das', 'e', 'f', 'g', 'k', 'kim', 'r', 's', 'sad']

[41]: l.sort(reverse=True)

[42]: print(l)

['sad', 's', 'r', 'kim', 'k', 'g', 'f', 'e', 'das', 'd', 'c', 'b', 'a']

[43]: l = [1,2,3,'a','d','b']
l.sort()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [43], in <cell line: 2>()
1 l = [1,2,3,'a','d','b']
----> 2 l.sort()

TypeError: '<' not supported between instances of 'str' and 'int'

[48]: lst = [1,2,30,4,5]


sorted(lst)

[48]: [1, 2, 4, 5, 30]

[47]: lst

[47]: [1, 2, 30, 4, 5]

• list.count() - frequency of a value in a list

7
[52]: l1 = [1,2,3,2,3,4,5,1,4,2,4,2,66,7,7]
print(l1.count(2))

4
• Shallow copy - what changes are made in the copied object are reflected in the original also
and viceversa
[53]: l1 = [1,2,3]
l2 = l1
l2.append(4)
print(l2)
print(l1)

[1, 2, 3, 4]
[1, 2, 3, 4]
• Deep copy or carbon copy - what ever changes made in the copied object will not reflect in
the original
• list.copy() is used for this

[54]: l1 = [1,3,5]
l2 = l1.copy()
l2.append(7)
print(l2)
print(l1)

[1, 3, 5, 7]
[1, 3, 5]

1.2 Tuples
• Tuples are similar to the lists but tuples are immutable which means we can not change the
elements of a tuple once assigned
• when we do not want to change the data over time. tuple is the prefered datatype
• iterating elements over a tuple is faster then iterating over list
• Empty Tuple
[4]: t = ()
print(t)
type(t)

()

[4]: tuple

[5]: t1 = tuple()
print(t1)
type(t1)

8
()

[5]: tuple

• Tuples are heterogeneous


[6]: t = (1,2.0,3+4j,True,'string',[1,3])
t

[6]: (1, 2.0, (3+4j), True, 'string', [1, 3])

• nested tuple
[8]: tup = (1,2,3,[3,4],(333,444),(43,56),'sri')
print(tup)

(1, 2, 3, [3, 4], (333, 444), (43, 56), 'sri')


• Tuple indexing
[9]: tup[0]

[9]: 1

[10]: tup[3][1]

[10]: 4

[11]: tup[4][0]

[11]: 333

[14]: tup[-2][-2]

[14]: 43

• tuple Slicing
[18]: tup[3:100]

[18]: ([3, 4], (333, 444), (43, 56), 'sri')

• Tuples are immutable


[19]: tup1 = (1,2,3,4,5,6,7)
print(tup1)

(1, 2, 3, 4, 5, 6, 7)

[20]: tup1[0]

9
[20]: 1

[21]: tup1[0] = 33

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [21], in <cell line: 1>()
----> 1 tup1[0] = 33

TypeError: 'tuple' object does not support item assignment

[22]: del tup1[3]

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [22], in <cell line: 1>()
----> 1 del tup1[3]

TypeError: 'tuple' object doesn't support item deletion

[23]: del tup1

[25]: print(tup1)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [25], in <cell line: 1>()
----> 1 print(tup1)

NameError: name 'tup1' is not defined

1.2.1 Interview questions

[28]: tup = (1,2,[3,4],(5,6,7))


tup

[28]: (1, 2, [3, 4], (5, 6, 7))

[30]: tup[3][0]

[30]: 5

[32]: tup[3][0]

10
[32]: 5

[33]: tup[3][0] = 50

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [33], in <cell line: 1>()
----> 1 tup[3][0] = 50

TypeError: 'tuple' object does not support item assignment

1.3 Tuple Methods


[34]: mytup = ('one','two','three','four','five','six','seven','eight','nine','ten')
print(mytup)

('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten')
• tuple.count() - it is used to find number of times an item occured in the tuple

[35]: mytup.count('one')

[35]: 1

[37]: mytup.index('one') # index of an element which is 'one'

[37]: 0

[43]: mytup1 = (1,3,2,4,7,6,5)


print(mytup1)

(1, 3, 2, 4, 7, 6, 5)

[44]: sorted(mytup1)

[44]: [1, 2, 3, 4, 5, 6, 7]

[45]: tuple(sorted(mytup1))

[45]: (1, 2, 3, 4, 5, 6, 7)

[46]: mytup1

[46]: (1, 3, 2, 4, 7, 6, 5)

[48]: mytup.sort()

11
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [48], in <cell line: 1>()
----> 1 mytup.sort()

AttributeError: 'tuple' object has no attribute 'sort'

1.4 Sets
• sets can be used to perform the mathematical operations like union, intersection and symetric
difference etc.
• Empty set
[52]: s1 = set()
print(s1)
type(s1)

set()

[52]: set

• A set is an unordered collections of items.


• Set is defined by values saperated by comma and inside the braces {}

[1]: a = {1,2,3.0,'a'}
print(a)

{1, 2, 3.0, 'a'}


• Every element is unique there are no duplicates
[4]: s = {10,20,30,40,20,10,30,50}
print(s)

{50, 20, 40, 10, 30}


• Set does not support indexing because it is unordered collection of items
[1]: print(s[1])

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 print(s[1])

NameError: name 's' is not defined

12
• creating set by using a list
[2]: l = [1,3,2,4,6,5,7]
print(l)
s1 = set(l)
print(s1)

[1, 3, 2, 4, 6, 5, 7]
{1, 2, 3, 4, 5, 6, 7}
• creating a set uning tuple
[3]: t = (1,2,3,4,5,6)
print(t)
s2 = set(t)
print(s2)

(1, 2, 3, 4, 5, 6)
{1, 2, 3, 4, 5, 6}

1.5 Set Methods


[4]: c = set()
c

[4]: set()

• set.add() we can add single element

[6]: c.add(3)
c

[6]: {3}

• set.update() add multiple elements

[11]: s = {1,2,3}
print(s)
s.update([5])
print(s)

{1, 2, 3}
{1, 2, 3, 5}
• set.copy() copy complete set

[14]: c = {4,5,6}
d = c.copy()
print(c)
print(d)

13
d.add(7)
print(d)

{4, 5, 6}
{4, 5, 6}
{4, 5, 6, 7}
• set.discard() remove a particular element if available

[15]: s = {1,2,3,4,5,6,7}
print(s)

{1, 2, 3, 4, 5, 6, 7}

[16]: s.discard(4)
print(s)

{1, 2, 3, 5, 6, 7}

[17]: s.discard(7)
print(s)

{1, 2, 3, 5, 6}
• set.remove() remove a particular value if available, else print error

[18]: s.remove(10)
print(s)

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 s.remove(10)
2 print(s)

KeyError: 10

• set.clear() remove all the elements in the set

[19]: s = {1,2,3,4,5}
s.clear()
print(s)

set()

[20]: s = {2,3,4,5,6,7}
del s
print(s)

14
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [20], in <cell line: 3>()
1 s = {2,3,4,5,6,7}
2 del s
----> 3 print(s)

NameError: name 's' is not defined

1.6 Set Operations


[21]: set1 = {1,2,3,4,5,6}
set2 = {3,4,5,6,7,8}
print(set1)
print(set2)

{1, 2, 3, 4, 5, 6}
{3, 4, 5, 6, 7, 8}
• Union
[22]: set1.union(set2)

[22]: {1, 2, 3, 4, 5, 6, 7, 8}

• Intersection
[23]: set1.intersection(set2)

[23]: {3, 4, 5, 6}

• difference: set of elements that are present only in set1 but not set2
[24]: set1 - set2

[24]: {1, 2}

[25]: set2 - set1

[25]: {7, 8}

• symmetric_difference() –> (A Union B) - (A Intersection B)

[28]: set1.symmetric_difference(set2)

[28]: {1, 2, 7, 8}

• issubset()

15
[30]: c = {1,2}
d = {1,2,3,4}
c.issubset(d)

[30]: True

[31]: d.issubset(c)

[31]: False

• issuperset()

[33]: d.issuperset(c)

[33]: True

[34]: c.issuperset(d)

[34]: False

1.7 Dictionary
• Dictionary is an unordered collection
• Empty Dictionary
[37]: d = {}
print(d)
type(d)

{}

[37]: dict

[38]: d = dict()
print(d)
type(d)

{}

[38]: dict

• in python the dictionaries are defined within braces {} with each item being key:value
• keys can be of fundamental data types - Generally keys are int or str
• values can be of any data type
[39]: dict = {1:2,4.5:[3,9,15],'key3':(94,8,23),'key4':{'item1','item2','item3'}}
dict

16
[39]: {1: 2,
4.5: [3, 9, 15],
'key3': (94, 8, 23),
'key4': {'item1', 'item2', 'item3'}}

[40]: car_type = {'car1':1989,'car2':123,'car3':{1:3}}


print(car_type)

{'car1': 1989, 'car2': 123, 'car3': {1: 3}}

[41]: car_type['car2'][1]

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [41], in <cell line: 1>()
----> 1 car_type['car2'][1]

TypeError: 'int' object is not subscriptable

[42]: car_type['car3'][1]

[42]: 3

[43]: marks = {'History':45,'Geography':54,'Hindi':56}


marks

[43]: {'History': 45, 'Geography': 54, 'Hindi': 56}

[44]: #total no. of items


len(marks)

[44]: 3

[46]: #items
marks.items()

[46]: dict_items([('History', 45), ('Geography', 54), ('Hindi', 56)])

[47]: #keys
marks.keys()

[47]: dict_keys(['History', 'Geography', 'Hindi'])

[48]: #values
marks.values()

[48]: dict_values([45, 54, 56])

17
1.7.1 Dictionary has indexing
• dict_name[key]

[49]: marks['History']

[49]: 45

• Dictionaries are mutable


• add a single item into dictionary
[51]: marks['English'] = 47
marks

[51]: {'History': 45, 'Geography': 54, 'Hindi': 56, 'english': 47, 'English': 47}

• add a multiple items into a dictionary


[52]: marks.update({'Chemistry':89,'Physics':98})
marks

[52]: {'History': 45,


'Geography': 54,
'Hindi': 56,
'english': 47,
'English': 47,
'Chemistry': 89,
'Physics': 98}

• replace values in dictionary


[54]: marks['Hindi'] = 64
marks

[54]: {'History': 45,


'Geography': 54,
'Hindi': 64,
'english': 47,
'English': 47,
'Chemistry': 89,
'Physics': 98}

• repace keys in dictionary


[55]: marks['Maths'] = marks.pop('english')
marks

[55]: {'History': 45,


'Geography': 54,

18
'Hindi': 64,
'English': 47,
'Chemistry': 89,
'Physics': 98,
'Maths': 47}

• delete item in dictionary


[56]: del marks['English']

[57]: marks

[57]: {'History': 45,


'Geography': 54,
'Hindi': 64,
'Chemistry': 89,
'Physics': 98,
'Maths': 47}

1.8 Range()
• We can generate a sequesnce of numbers using range() function. range(10) will generate
numbers from 0 to 9(10 numbers).
• We can also define the start, stop and step size as range(start,stop,step size). step size defaults
to 1 if not provided.
• This function does not store all the values in memory. it would be insufficient. so it remembers
the start, stop step size and generates the next number on the go.
• To force theis function to output all the items, we can use the function list()
• The following example will clarify this.
• We can use the range() function in for loops to iterate through a sequence of numbers. It can
be combined with the len() function to iterate through a sequence using indixing. Here is the
example.
[58]: range(10)

[58]: range(0, 10)

[59]: list(range(10))

[59]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[60]: tuple(range(10))

[60]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

[61]: list(range(8,11))

[61]: [8, 9, 10]

19
[62]: list(range(0,11,2))

[62]: [0, 2, 4, 6, 8, 10]

[63]: list(range(8,0,-1))

[63]: [8, 7, 6, 5, 4, 3, 2, 1]

[ ]:

20

You might also like