Dictionary in Pyhton
In python, a dictionary is a mixed collection of elements. Unlike other collection data types such as a list or tuple, the dictionary type stores a key along with its element. The keys in a Python dictionary is separated by a colon ( : ) while the commas work as a separator for the elements. The key value pairs are enclosed with curly braces { }.
Syntax of defining a dictionary:
Dictionary_Name = { Key_1: Value_1,
Key_2:Value_2,
……..
Key_n:Value_n
}
Key in the dictionary must be unique case sensitive and can be of any valid Python type.
Creating a Dictionary
# Empty dictionary
Dict1 = { }
# Dictionary with Key
Dict_Stud = { RollNo: 1234, Name:Murali, Class:XII, Marks:451}
Dictionary Comprehensions
In Python, comprehension is another way of creating dictionary. The following is the syntax of creating such dictionary.
Syntax
Dict = { expression for variable in sequence [if condition] }
The if condition is optional and if specified, only those values in the sequence are evaluated using the expression which satisfy the condition.
Example
Dict = { x : 2 * x for x in range(1,10)}
Output of the above code is
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
Accessing, Adding, Modifying and Deleting elements from a Dictionary
Accessing all elements from a dictionary is very similar as Lists and Tuples. Simple print function is used to access all the elements. If you want to access a particular element, square brackets can be used along with key.
Example : Program to access all the values stored in a dictionary
MyDict = { 'Reg_No': '1221',
'Name' : 'Tamilselvi',
'School' : 'CGHSS',
'Address' : 'Rotler St., Chennai 112' }
print(MyDict)
print("Register Number: ", MyDict['Reg_No'])
print("Name of the Student: ", MyDict['Name'])
print("School: ", MyDict['School'])
print("Address: ", MyDict['Address'])
Output:
{'Reg_No': '1221', 'Name': 'Tamilselvi', 'School': 'CGHSS', 'Address': 'Rotler St., Chennai 112'}
Register Number: 1221
Name of the Student: Tamilselvi
School: CGHSS
Address: Rotler St., Chennai 112
Note that, the first print statement prints all the values of the dictionary. Other statements are printing only the specified values which is given within square brackets.
In an existing dictionary, you can add more values by simply assigning the value along with key. The following syntax is used to understand adding more elements in a dictionary.
dictionary_name [key] = value/element
Example : Program to add a new value in the dictionary
MyDict = { 'Reg_No': '1221',
'Name' : 'Tamilselvi',
'School' : 'CGHSS', 'Address' : '
Rotler St., Chennai 112'}
print(MyDict)
print("Register Number: ", MyDict['Reg_No'])
print("Name of the Student: ", MyDict['Name'])
MyDict['Class'] = 'XII - A' # Adding new value
print("Class: ", MyDict['Class']) # Printing newly added value
print("School: ", MyDict['School'])
print("Address: ", MyDict['Address'])
Modification of a value in dictionary is very similar as adding elements. When you assign a value to a key, it will simply overwrite the old value.
In Python dictionary, del keyword is used to delete a particular element. The clear( ) function is used to delete all the elements in a dictionary. To remove the dictionary, you can use del keyword with dictionary name.
Syntax:
# To delete a particular element.
del dictionary_name[key]
# To delete all the elements
dictionary_name.clear( )
# To delete an entire dictionary
del dictionary_name
Example : Program to delete elements from a dictionary and finally deletes the dictionary.
Dict = {'Roll No' : 12001, 'SName' : 'Meena', 'Mark1' : 98, 'Marl2' : 86}
print("Dictionary elements before deletion: \n", Dict)
del Dict['Mark1'] # Deleting a particular element
print("Dictionary elements after deletion of a element: \n", Dict)
Dict.clear() # Deleting all elements
print("Dictionary after deletion of all elements: \n", Dict)
del Dict
print(Dict) # Deleting entire dictionary
Output:
Dictionary elements before deletion:
{'Roll No': 12001, 'SName': 'Meena', 'Mark1': 98, 'Marl2': 86}
Dictionary elements after deletion of a element:
{'Roll No': 12001, 'SName': 'Meena', 'Marl2': 86}
Dictionary after deletion of all elements:
{ }
Traceback (most recent call last):
File "E:/Python/Dict_Test_02.py", line 8, in <module>
print(Dict)
NameError: name 'Dict' is not defined
Difference between List and Dictionary
(1) List is an ordered set of elements. But, a dictionary is a data structure that is used for matching one element (Key) with another (Value).
(2) The index values can be used to access a particular element. But, in dictionary key represents index. Remember that, key may be a number of a string.
(3) Lists are used to look up a value whereas a dictionary is used to take one value and look up another value.
Qus. 1 : <p>What will be the output of the following Python code?</p><pre>d = {0: 'a', 1: 'b', 2: 'c'}<br>for i in d:<br> print(i)</pre>
- 0 1 2
- a b c
- 0 a 1 b 2 c
- none of the mentioned
Qus. 2 : <p>What will be the output of the following Python code?</p><pre><span style="font-size: 14px;">d = {0: 'a', 1: 'b', 2: 'c'}<br></span><span style="font-size: 14px;">for x, y in d.items():<br></span><span style="font-size: 14px;"> print(x, y)</span></pre><div>A)</div><div><br></div><div>0</div><div>1</div><div>2</div><div><br></div><div>B)</div><div><br></div><div>a</div><div>b</div><div>c</div><div><br></div><div>C)</div><div>0 a</div><div>1 b</div><div>2 c</div><div><br></div><div>D)</div><div>None of the Above</div><div><br></div>
- Option A
- Option B
- Option C
- Option D
Qus. 3 : <p>What will be the output of the following Python code?</p><pre>d = {0: 'a', 1: 'b', 2: 'c'}<br>for x in d.keys():<br> print(d[x])</pre>
- 0 1 2
- a b c
- 0 a 1 b 2 c
- none of the mentioned
Qus. 4 : <p>What will be the output of the following Python code?</p><pre>d = {0: 'a', 1: 'b', 2: 'c'}<br>for x in d.values():<br> print(x)</pre><div><br></div>
- 0 1 2
- a b c
- 0 a 1 b 2 c
- none of the mentioned
Qus. 5 : Which of the following statements create a dictionary?
- d = {}
- d = {“john”:40, “peter”:45}
- d = {40:”john”, 45:”peter”}
- All of the mentioned
Qus. 6 : <p>What will be the output of the following Python code snippet?</p><pre><span style="font-size: 14px;">d = {"john":40, "peter":45}<br></span><span style="font-size: 14px;">print("john" in d)</span></pre><div><br></div> <p> </p>
- True
- False
- None
- Error
Qus. 7 : <p>What will be the output of the following Python code snippet?</p><pre><span style="font-size: 14px;">d1 = {"john":40, "peter":45}<br></span><span style="font-size: 14px;">d2 = {"john":466, "peter":45}<br></span><span style="font-size: 14px;">print(d1 == d2)</span></pre> <p> </p>
- True
- False
- None
- Error
Qus. 8 : <p>What will be the output of the following Python code snippet?</p><pre><span style="font-size: 14px;">d1 = {"john":40, "peter":45}<br></span><span style="font-size: 14px;">d2 = {"john":466, "peter":45}<br></span><span style="font-size: 14px;">print(d1 > d2)</span></pre> <p> </p> <p> </p> <p> </p>
- True
- False
- Error
- None
Qus. 9 : <p>What will be the output of the following Python code snippet?</p><pre><span style="font-size: 14px;">d = {"john":40, "peter":45}<br></span><span style="font-size: 14px;">print(d["john"])</span></pre><div><br></div>
- 40
- 45
- “john”
- “peter”
Qus. 10 : Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?
- d.delete(“john”:40)
- d.delete(“john”)
- del d[“john”]
- del d(“john”:40)
Qus. 11 : Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?
- d.size()
- len(d)
- size(d)
- d.len()
Qus. 12 : <p>What will be the output of the following Python code snippet?</p> <pre> <span style="font-size: 14px;">d = {"john":40, "peter":45}<br></span><span style="font-size: 14px;">print(list(d.keys()))</span></pre>
- [“john”, “peter”]
- [“john”:40, “peter”:45]
- (“john”, “peter”)
- (“john”:40, “peter”:45)
Qus. 13 : Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“susan”]?
- Since “susan” is not a value in the set, Python raises a KeyError exception
- It is executed fine and no exception is raised, and it returns None
- Since “susan” is not a key in the set, Python raises a KeyError exception
- Since “susan” is not a key in the set, Python raises a syntax error
Qus. 14 : Which of these about a dictionary is false?
- The values of a dictionary can be accessed using keys
- The keys of a dictionary can be accessed using values
- Dictionaries aren’t ordered
- Dictionaries are mutable
Qus. 15 : Which of the following is not a declaration of the dictionary?
- {1: ‘A’, 2: ‘B’}
- dict([[1,”A”],[2,”B”]])
- {1,”A”,2”B”}
- { }
Qus. 16 : <p>What will be the output of the following Python code snippet?</p><pre><span style="font-size: 14px;">a={1:"A",2:"B",3:"C"}<br></span><span style="font-size: 14px;">for i,j in a.items(): <br></span><span style="font-size: 14px;"> print(i,j,end=" ")</span></pre><div><br></div>
- 1 A 2 B 3 C
- 1 2 3
- A B C
- 1:”A” 2:”B” 3:”C”
Qus. 17 : <p>What will be the output of the following Python code snippet?</p> <pre><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">a</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">=</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">{</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">1</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">:</span><span class="st0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(72,="" 61,="" 139);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">"A"</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">2</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">:</span><span class="st0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(72,="" 61,="" 139);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">"B"</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">3</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">:</span><span class="st0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(72,="" 61,="" 139);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">"C"</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">}<br></span><span class="kw1" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 119,="" 0);="" font-weight:="" bold;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">print</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">(</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">a.</span><span class="me1" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">get</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">(</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">1</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">4</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">)</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">)</span></pre>
- 1
- A
- 4
- Invalid syntax for get method
Qus. 18 : <p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">a={1:"A",2:"B",3:"C"}<br></font><font color="#333333">print(a.get(3))</font></pre>
- Error, invalid syntax
- A
- 5
- C
Qus. 19 : <p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">a={1:"A",2:"B",3:"C"}<br></font><font color="#333333">a.setdefault(4,"D") <br></font><font color="#333333">print(a)</font></pre><div><br></div>
- {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
- None
- Error
- [1,3,6,10]
Qus. 20 : <p>What will be the output of the following Python code?</p> <pre><font color="#333333">a={1:"A",2:"B",3:"C"}<br></font><font color="#333333">b={4:"D",5:"E"}<br></font><font color="#333333">a.update(b) <br></font><font color="#333333">print(a)</font></pre>
- {1: ‘A’, 2: ‘B’, 3: ‘C’}
- Method update() doesn’t exist for dictionaries
- {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
- {4: ‘D’, 5: ‘E’}
Qus. 21 : <p>What will be the output of the following Python code?</p> <pre><font color="#333333">a={1:"A",2:"B",3:"C"}<br></font><font color="#333333">b=a.copy()<br></font><font color="#333333">b[2]="D" <br></font><font color="#333333">print(a)</font></pre>
- Error, copy() method doesn’t exist for dictionaries
- {1: ‘A’, 2: ‘B’, 3: ‘C’}
- {1: ‘A’, 2: ‘D’, 3: ‘C’}
- “None” is printed
Qus. 22 : <p>What will be the output of the following Python code?</p> <pre><font color="#333333">a={1:"A",2:"B",3:"C"}<br></font><font color="#333333">a.clear() <br></font><font color="#333333">print(a)</font></pre>
- None
- { None:None, None:None, None:None}
- {1:None, 2:None, 3:None}
- { }
Qus. 23 : Which of the following isn’t true about dictionary keys?
- More than one key isn’t allowed
- Keys must be immutable
- Keys must be integers
- When duplicate keys encountered, the last assignment wins
Qus. 24 : <p>What will be the output of the following Python code?</p> <pre><font color="#333333">a={1:5,2:3,3:4}<br></font><font color="#333333">a.pop(3) <br></font><font color="#333333">print(a)</font></pre>
- {1: 5}
- {1: 5, 2: 3}
- Error, syntax error for pop() method
- {1: 5, 3: 4}
Qus. 25 : <p>What will be the output of the following Python code?</p> <pre><font color="#333333">a={1:"A",2:"B",3:"C"}<br></font><font color="#333333">for i in a: <br></font><font color="#333333"> print(i,end=" ")</font></pre>
- 1 2 3
- ‘A’ ‘B’ ‘C’
- 1 ‘A’ 2 ‘B’ 3 ‘C’
- Error, it should be: for i in a.items():
Qus. 26 : <p>What will be the output of the following Python code?</p> <pre>a={1:"A",2:"B",3:"C"}<br>print(a.items())</pre>
- Syntax error
- dict_items([(‘A’), (‘B’), (‘C’)])
- dict_items([(1,2,3)])
- dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
Qus. 27 : Which of the statements about dictionary values if false?
- More than one key can have the same value
- The values of the dictionary can be accessed as dict[key]
- Values of a dictionary must be unique
- Values of a dictionary can be a mixture of letters and numbers
Qus. 28 : <p>What will be the output of the following Python code snippet?</p> <pre><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">>>></span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"=""> a</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">=</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">{</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">1</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">:</span><span class="st0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(72,="" 61,="" 139);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">"A"</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">2</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">:</span><span class="st0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(72,="" 61,="" 139);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">"B"</span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">,</span><span class="nu0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 69,="" 0);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">3</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"="">:</span><span class="st0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(72,="" 61,="" 139);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">"C"</span><span class="br0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">}<br></span><span class="sy0" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(102,="" 204,="" 102);="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">>>></span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"=""> </span><span class="kw1" style="font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;="" overflow:="" visible;="" padding:="" 0px;="" border:="" box-shadow:="" none;="" background-image:="" initial;="" background-position:="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" overflow-wrap:="" normal;="" color:="" rgb(255,="" 119,="" 0);="" font-weight:="" bold;="" margin:="" 0px="" !important;="" line-height:="" 20px="" !important;"="">del</span><span style="background-color: rgb(244, 244, 244); color: rgb(51, 51, 51); font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" text-align:="" justify;"=""> a</span></pre>
- method del doesn’t exist for the dictionary
- del deletes the values in the dictionary
- del deletes the entire dictionary
- del deletes the keys in the dictionary
Qus. 29 : If a is a dictionary with some key-value pairs, what does a.popitem() do?
- Removes an arbitrary element
- Removes all the key-value pairs
- Removes the key-value pair for the key given as an argument
- Invalid method for dictionary
Qus. 30 : <p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">total={}<br></font><font color="#333333">def insert(items):<br></font><font color="#333333"> if items in total:<br></font><font color="#333333"> total[items] += 1<br></font><font color="#333333"> else:<br></font><font color="#333333"> total[items] = 1<br></font><font color="#333333">insert('Apple')<br></font><font color="#333333">insert('Ball')<br></font><font color="#333333">insert('Apple') <br></font><font color="#333333">print (len(total))</font></pre><div><br></div>
- 3
- 1
- 2
- 0
Qus. 31 : <p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">a = {}<br></font><font color="#333333">a[1] = 1<br></font><font color="#333333">a['1'] = 2<br></font><font color="#333333">a[1]=a[1]+1<br></font><font color="#333333">count = 0<br></font><font color="#333333">for i in a: <br></font><font color="#333333"> count += a[i]<br></font><font color="#333333">print(count)</font></pre>
- 1
- 2
- 4
- Error, the keys can’t be a mixture of letters and numbers
Qus. 32 : <p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">numbers = {}<br></font><font color="#333333">letters = {}<br></font><font color="#333333">comb = {}<br></font><font color="#333333">numbers[1] = 56<br></font><font color="#333333">numbers[3] = 7<br></font><font color="#333333">letters[4] = 'B'<br></font><font color="#333333">comb['Numbers'] = numbers <br></font><font color="#333333">comb['Letters'] = letters<br></font><font color="#333333">print(comb)</font></pre><div><br></div>
- Error, dictionary in a dictionary can’t exist
- ‘Numbers’: {1: 56, 3: 7}
- {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
- {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}
Qus. 33 : <p>What will be the output of the following Python code snippet?</p> <pre>test = {1:'A', 2:'B', 3:'C'}<br>test = {} <br>print(len(test))</pre>
- 0
- None
- 3
- An exception is thrown
Qus. 34 : <p>What will be the output of the following Python code snippet?</p><pre>test = {1:'A', 2:'B', 3:'C'}<br>del test[1]<br>test[1] = 'D'<br>del test[2]<br>print(len(test))</pre>
- 0
- 2
- Error as the key-value pair of 1:’A’ is already deleted
- 1
Qus. 35 : <p>What will be the output of the following Python code snippet?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""> <span style="font-size: 12.6px;">a = {}
a[1] = 1
a['1'] = 2
a[1.0]=4
count = 0
for i in a:
count += a[i]
print(count)
print(a)</span></pre>
- An exception is thrown
- 3
- 6
- 2
Qus. 36 : <p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">a={}<br></font><font color="#333333">a['a']=1<br></font><font color="#333333">a['b']=[2,3,4] <br></font><font color="#333333">print(a)</font></pre><div><br></div>
- Exception is thrown
- {‘b’: [2], ‘a’: 1}
- {‘b’: [2], ‘a’: [3]}
- {'a': 1, 'b': [2, 3, 4]}
Qus. 37 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""><span style="font-size: 12.6px;">count={}
count[(1,2,4)] = 5
count[(4,2,1)] = 7
count[(1,2)] = 6
count[(4,2,1)] = 2
tot = 0
for i in count:
tot=tot+count[i]
print(len(count)+tot)</span>
</pre><div><br></div>
- 25
- 17
- 16
- Tuples can’t be made keys of a dictionary
Qus. 38 : <p>What will be the output of the following Python code?</p> <pre class="de1" style="border-radius: 0px; padding: 0px; font-family: Consolas, Monaco, " lucida="" console",="" monospace;="" color:="" rgb(51,="" 51,="" 51);="" word-break:="" break-all;="" overflow-wrap:="" normal;="" background:="" rgb(244,="" 244,="" 244);="" border:="" 0px;="" overflow:="" visible;="" box-shadow:="" none;="" text-align:="" justify;="" margin-top:="" 0px="" !important;="" margin-bottom:="" line-height:="" 20px="" !important;"=""><span style="font-size: 12.6px;">a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])</span><br></pre>
- [2,3,4]
- 3
- 2
- An exception is thrown
Qus. 39 : <p>What will be the output of the following Python code?</p> <pre>a={'B':5,'A':9,'C':7}<br>print(sorted(a))</pre>
- [‘A’,’B’,’C’]
- [‘B’,’C’,’A’]
- [5,7,9]
- [9,5,7]
Qus. 40 : If b is a dictionary, what does any(b) do?
- Returns True if any key of the dictionary is true
- Returns False if dictionary is empty
- Returns True if all keys of the dictionary are true
- Method any() doesn’t exist for dictionary
Qus. 41 : <p>What will be the output of the following Python code?</p> <pre>a=dict()<br>print(a[1])</pre><div><br></div>
- An exception is thrown since the dictionary is empty
- ‘ ‘
- 1
- 0
Qus. 42 : <p>What is the output of the following code?</p><pre>dict={"Joey":1, "Rachel":2}<br>dict.update({"Phoebe":2})<br>print(dict)</pre>
- {'Joey': 1, 'Rachel': 2, 'Phoebe': 2}
- {“Joey”:1,”Rachel”:}
- {“Joey”:1,”Phoebe”:2}
- Error
Qus. 43 : <p>What is the output of the following code?</p><pre>a = {1:"A", 2: "B", 3: "C"}<br>b = {4: "D", 5: "E"}<br>a.update(b)<br>print(a)</pre>
- {1:’A’, 2: ‘B’,3: ‘C’}
- {1: ‘A’, 2: ‘b’, 3: ‘c’, 4: ‘D’, 5: ‘E’}
- Error
- {4: ‘D’, 5: ‘E’}
Qus. 44 : <p>Which of the following will delete key-value pair for key="tiger" in dictionary?</p><pre>dic={"lion":"'wild","tiger":"'wild",'cat":"domestic",<span style="font-size: 1rem; letter-spacing: 0.01rem;">"dog":"domestic"}</span></pre>
- del dic("tiger"]
- dic["tiger"].delete()
- delete(dic.["tiger'])
- del(dic.["tiger"])
Qus. 45 : <p>What will be the output of the following Python code?</p><pre>d1={"abc":5,"def":6,"ghi":7}<br>print(d1[0])</pre>
- abc
- 5
- {"abc":5}
- error
Qus. 46 : Which of the following function of dictionary gets all the keys from the dictionary?
- getkeys ()
- key()
- keys()
- None
Qus. 47 : Which keyword is used to remove individual items or the entire dictionary itself.
- del
- remove
- removeAll
- None of these
Qus. 48 : Suppose d = {"john”:40, “peter":45}. To obtain the number of entries in dictionary which command do we use?
- d.size()
- len(d)
- size(d)
- d.len ()
Qus. 49 : Which of the following is a mapping datatype?
- String
- List
- Tuple
- Dictionary
Qus. 50 : Which one is not a built in function
- dictionary()
- set()
- tuple()
- list()
Qus. 51 : Dictionary has:
- Sequence value pair
- Key value pair
- Tuple value pair
- Record value pair