Tuple & Dict
Tuple & Dict
35 / 52
Tuples
Operation Example
Multiplicationiii t1 ∗ 2
Operator ∗ (’Monday’, ’Tuesday’, ’Monday’, ’Tuesday’)
iii t3=t1 + (’Wednesday’,)
Concatenation
iii t3
Operator +
(’Monday’, ’Tuesday’, ’Wednesday’)
Length iii len(t1)
Operator len 2
iii t2[-2]
Indexing
20
37 / 52
Tuple Operations
Operation Example
Slicing Syntax: iii t1[1:2]
start:end:inc (’Tuesday’, )
iii min(t2)
Function min
10
iii max(t2)
Function max
30
Function sum iii sum(t2)
(not defined on strings) 60
iii ’Friday’ in t1
Membership operator in
False
Table 4: Summary of operations that can be applied on tuples
38 / 52
Functions Tuple and Zip
39 / 52
Functions count and index
Function Explanation
T.count(e) Returns count of occurrences of e in Tuple T
T.index(e) Returns index of first occurrences of e in Tuple T
Table 5: Tuple Functions
40 / 52
Dictionary
41 / 52
Dictionary
iii price = {’tomato’:40, ’cucumber’:30, ’potato’:20,
’cauliflower’:70, ’cabbage’:50, ’lettuce’:40, ’raddish’:30, ’carrot’:20,
’peas’:80}
iii price[’potato’]
20
iii price[’carrot’]
20
iii price.keys()
dict keys([’tomato’, ’cucumber’, ’potato’, ’cauliflower’, ’cabbage’,
’lettuce’, ’raddish’, ’carrot’, ’peas’])
iii price.values()
dict values([40, 30, 20, 70, 50, 40, 30, 20, 80])
iii price.items()
dict items([(’tomato’, 40), (’cucumber’, 30), (’potato’, 20),
(’cauliflower’, 70), (’cabbage’, 50), (’lettuce’, 40), (’raddish’, 30),
(’carrot’, 20), (’peas’,80)])
42 / 52
Dictionary
43 / 52
Dictionary Operations
some operations that can be applied to a dictionary and illustrate
these operations using a dictionary of digit-name pairs:
digits = {0:’Zero’, 1:’One’, 2:’Two’, 3:’Three’, 4:’Four’, 5:’Five’,
6:’Six’, 7:’Seven’, 8:’Eight’, 9:’Nine’}
Operation Examples
Length operator len (number of iii len(digits)
key-value pairs in dictionary) 10
iii digits[1]
Indexing
’One’
iii min(digits)
Function min
0
iii max(digits)
Function max
9
Function sum (assuming keys iii sum(digits)
are compatible for addition) 45
44 / 52
Dictionary Operations
Operation Examples
iii 5 in digits
True
Membership operator in
iii ’Five’ in digits
False
Table 6: Summary of operations that can be applied on dictionaries
45 / 52
Dictionary Operations
46 / 52
Dictionary Functions
Function Explanation
D.items() Returns an object comprising of tuples of
key-value pairs present in dictionary D
D.keys() Returns an object comprising of all keys of
dictionary D
D.values() Returns an object comprising of all values
of dictionary D
D.clear() Removes all key-value pairs from dictio-
nary D
D.get(key, default) For the specified key, the function returns
the associated value. Returns the default
value in the case key is not present in the
dictionary D
D.copy() Creates a shallow copy of dictionary D
D1.update(D2) Adds the key-value pairs of dictionary D2
to dictionary D1
Table 7: Dictionary Functions 47 / 52
Inverted Dictionary
48 / 52
Inverted Dictionary Code
1 def buildInvDict(dict1):
2 ’’’
3 Objective: To construct inverted dictionary
4 Input parameter: dict1: dictionary
5 Return value: invDict: dictionary
6 ’’’
7 invDict = {}
8 for key,values in dict1.items():
9 if value in invDict:
10 invDict[value].append(key)
11 else:
12 invDict[value] = [key]
13 invDict={x:invDict[x] for x in invDict if len(invDict[x]>1)
}
14 return invDict
49 / 52
Inverted Dictionary Code
1 def main():
2 ’’’
3 Objective: To find inverted dictionary
4 Input parameters: None
5 Return value: None
6 ’’’
7 wordMeaning = eval(input(’Enter word meaning dictionary:’))
8 meaningWord = buildInvDict(wordMeaning)
9 print(’Inverted dictionary:\n’, meaningWord)
10
11 # Statements to initiate the call to main function.
12
13 if __name__==’__main__’:
14 main()
50 / 52