Professional Documents
Culture Documents
S or Data Structures or Containers
S or Data Structures or Containers
[1]: []
[2]: type(l)
[2]: list
[3]: l1 = list()
l1
[3]: []
[4]: type(l1)
[4]: list
[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
[9]: 3
• Nested List
[10]: nest = [1,2,[3,4],5]
nest
[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]
[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
[24]: a[2][2][1][3]
[24]: 'l'
[25]: a[1]
[25]: 2
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]
• List Concatenation
3
[1]: l1 = ['dsad','b','c']
l2 = ['a',6,4.0]
l3 = l1+l2
l3
• 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]
[1, 2, 3, 4]
• 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
• list.insert() - to insert in a specific order and list.insert(x,y) will add item y at location x
[3]: lst1
[3]: [1, 2, 3, 4]
[4]: lst1.remove(3)
[5]: lst1
[5]: [1, 2, 4]
[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
• 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
[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
[34]: ls1[1].reverse()
[35]: ls1
6
• list.sort() - it is used to sort the list in ascending order or the descending order
[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()
[47]: lst
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
• nested tuple
[8]: tup = (1,2,3,[3,4],(333,444),(43,56),'sri')
print(tup)
[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]
(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 Traceback (most recent call last)
Input In [22], in <cell line: 1>()
----> 1 del tup1[3]
[25]: print(tup1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [25], in <cell line: 1>()
----> 1 print(tup1)
[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
('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]: 0
(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()
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
[1]: a = {1,2,3.0,'a'}
print(a)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 print(s[1])
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}
[4]: set()
[6]: c.add(3)
c
[6]: {3}
[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
[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)
{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]: {7, 8}
[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'}}
[41]: car_type['car2'][1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [41], in <cell line: 1>()
----> 1 car_type['car2'][1]
[42]: car_type['car3'][1]
[42]: 3
[44]: 3
[46]: #items
marks.items()
[47]: #keys
marks.keys()
[48]: #values
marks.values()
17
1.7.1 Dictionary has indexing
• dict_name[key]
[49]: marks['History']
[49]: 45
[51]: {'History': 45, 'Geography': 54, 'Hindi': 56, 'english': 47, 'English': 47}
18
'Hindi': 64,
'English': 47,
'Chemistry': 89,
'Physics': 98,
'Maths': 47}
[57]: marks
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)
[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))
19
[62]: list(range(0,11,2))
[63]: list(range(8,0,-1))
[63]: [8, 7, 6, 5, 4, 3, 2, 1]
[ ]:
20