Dictionary in Pyhton

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>&nbsp; &nbsp; print(i)</pre>

  1. 0 1 2
  2. a b c
  3. 0 a 1 b 2 c
  4. 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;">&nbsp; &nbsp; 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>

  1. Option A
  2. Option B
  3. Option C
  4. 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>&nbsp; &nbsp; print(d[x])</pre>

  1. 0 1 2
  2. a b c
  3. 0 a 1 b 2 c
  4. 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>&nbsp; &nbsp; print(x)</pre><div><br></div>

  1. 0 1 2
  2. a b c
  3. 0 a 1 b 2 c
  4. none of the mentioned
Qus. 5 : Which of the following statements create a dictionary?

  1. d = {}
  2. d = {“john”:40, “peter”:45}
  3. d = {40:”john”, 45:”peter”}
  4. 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>&nbsp;</p>

  1. True
  2. False
  3. None
  4. 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>&nbsp;</p>

  1. True
  2. False
  3. None
  4. 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 &gt; d2)</span></pre> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>

  1. True
  2. False
  3. Error
  4. 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>

  1. 40
  2. 45
  3. “john”
  4. “peter”
Qus. 10 : Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?

  1. d.delete(“john”:40)
  2. d.delete(“john”)
  3. del d[“john”]
  4. 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?

  1. d.size()
  2. len(d)
  3. size(d)
  4. d.len()
Qus. 12 : <p>What will be the output of the following Python code snippet?</p> <pre>&nbsp;<span style="font-size: 14px;">d = {"john":40, "peter":45}<br></span><span style="font-size: 14px;">print(list(d.keys()))</span></pre>

  1. [“john”, “peter”]
  2. [“john”:40, “peter”:45]
  3. (“john”, “peter”)
  4. (“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”]?

  1. Since “susan” is not a value in the set, Python raises a KeyError exception
  2. It is executed fine and no exception is raised, and it returns None
  3. Since “susan” is not a key in the set, Python raises a KeyError exception
  4. Since “susan” is not a key in the set, Python raises a syntax error
Qus. 14 : Which of these about a dictionary is false?

  1. The values of a dictionary can be accessed using keys
  2. The keys of a dictionary can be accessed using values
  3. Dictionaries aren’t ordered
  4. Dictionaries are mutable
Qus. 15 : Which of the following is not a declaration of the dictionary?

  1. {1: ‘A’, 2: ‘B’}
  2. dict([[1,”A”],[2,”B”]])
  3. {1,”A”,2”B”}
  4. { }
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():&nbsp;<br></span><span style="font-size: 14px;">&nbsp; &nbsp; print(i,j,end=" ")</span></pre><div><br></div>

  1. 1 A 2 B 3 C
  2. 1 2 3
  3. A B C
  4. 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. 1
  2. A
  3. 4
  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>

  1. Error, invalid syntax
  2. A
  3. 5
  4. 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")&nbsp;<br></font><font color="#333333">print(a)</font></pre><div><br></div>

  1. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
  2. None
  3. Error
  4. [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)&nbsp;<br></font><font color="#333333">print(a)</font></pre>

  1. {1: ‘A’, 2: ‘B’, 3: ‘C’}
  2. Method update() doesn’t exist for dictionaries
  3. {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
  4. {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"&nbsp;<br></font><font color="#333333">print(a)</font></pre>

  1. Error, copy() method doesn’t exist for dictionaries
  2. {1: ‘A’, 2: ‘B’, 3: ‘C’}
  3. {1: ‘A’, 2: ‘D’, 3: ‘C’}
  4. “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()&nbsp;<br></font><font color="#333333">print(a)</font></pre>

  1. None
  2. { None:None, None:None, None:None}
  3. {1:None, 2:None, 3:None}
  4. { }
Qus. 23 : Which of the following isn’t true about dictionary keys?

  1. More than one key isn’t allowed
  2. Keys must be immutable
  3. Keys must be integers
  4. 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)&nbsp;<br></font><font color="#333333">print(a)</font></pre>

  1. {1: 5}
  2. {1: 5, 2: 3}
  3. Error, syntax error for pop() method
  4. {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:&nbsp;<br></font><font color="#333333">&nbsp; &nbsp; print(i,end=" ")</font></pre>

  1. 1 2 3
  2. ‘A’ ‘B’ ‘C’
  3. 1 ‘A’ 2 ‘B’ 3 ‘C’
  4. 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>

  1. Syntax error
  2. dict_items([(‘A’), (‘B’), (‘C’)])
  3. dict_items([(1,2,3)])
  4. dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
Qus. 27 : Which of the statements about dictionary values if false?

  1. More than one key can have the same value
  2. The values of the dictionary can be accessed as dict[key]
  3. Values of a dictionary must be unique
  4. 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;"="">&gt;&gt;&gt;</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;"="">&gt;&gt;&gt;</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>

  1. method del doesn’t exist for the dictionary
  2. del deletes the values in the dictionary
  3. del deletes the entire dictionary
  4. del deletes the keys in the dictionary
Qus. 29 : If a is a dictionary with some key-value pairs, what does a.popitem() do?

  1. Removes an arbitrary element
  2. Removes all the key-value pairs
  3. Removes the key-value pair for the key given as an argument
  4. 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">&nbsp; &nbsp; if items in total:<br></font><font color="#333333">&nbsp; &nbsp; &nbsp; &nbsp; total[items] += 1<br></font><font color="#333333">&nbsp; &nbsp; else:<br></font><font color="#333333">&nbsp; &nbsp; &nbsp; &nbsp; total[items] = 1<br></font><font color="#333333">insert('Apple')<br></font><font color="#333333">insert('Ball')<br></font><font color="#333333">insert('Apple')&nbsp;<br></font><font color="#333333">print (len(total))</font></pre><div><br></div>

  1. 3
  2. 1
  3. 2
  4. 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:&nbsp;<br></font><font color="#333333">&nbsp; &nbsp; count += a[i]<br></font><font color="#333333">print(count)</font></pre>

  1. 1
  2. 2
  3. 4
  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&nbsp;<br></font><font color="#333333">comb['Letters'] = letters<br></font><font color="#333333">print(comb)</font></pre><div><br></div>

  1. Error, dictionary in a dictionary can’t exist
  2. ‘Numbers’: {1: 56, 3: 7}
  3. {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
  4. {‘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 = {}&nbsp;<br>print(len(test))</pre>

  1. 0
  2. None
  3. 3
  4. 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>

  1. 0
  2. 2
  3. Error as the key-value pair of 1:’A’ is already deleted
  4. 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>

  1. An exception is thrown
  2. 3
  3. 6
  4. 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]&nbsp;<br></font><font color="#333333">print(a)</font></pre><div><br></div>

  1. Exception is thrown
  2. {‘b’: [2], ‘a’: 1}
  3. {‘b’: [2], ‘a’: [3]}
  4. {'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>

  1. 25
  2. 17
  3. 16
  4. 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>

  1. [2,3,4]
  2. 3
  3. 2
  4. 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>

  1. [‘A’,’B’,’C’]
  2. [‘B’,’C’,’A’]
  3. [5,7,9]
  4. [9,5,7]
Qus. 40 : If b is a dictionary, what does any(b) do?

  1. Returns True if any key of the dictionary is true
  2. Returns False if dictionary is empty
  3. Returns True if all keys of the dictionary are true
  4. 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>

  1. An exception is thrown since the dictionary is empty
  2. ‘ ‘
  3. 1
  4. 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>

  1. {'Joey': 1, 'Rachel': 2, 'Phoebe': 2}
  2. {“Joey”:1,”Rachel”:}
  3. {“Joey”:1,”Phoebe”:2}
  4. 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. {1:’A’, 2: ‘B’,3: ‘C’}
  2. {1: ‘A’, 2: ‘b’, 3: ‘c’, 4: ‘D’, 5: ‘E’}
  3. Error
  4. {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>

  1. del dic("tiger"]
  2. dic["tiger"].delete()
  3. delete(dic.["tiger'])
  4. 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>

  1. abc
  2. 5
  3. {"abc":5}
  4. error
Qus. 46 : Which of the following function of dictionary gets all the keys from the dictionary?

  1. getkeys ()
  2. key()
  3. keys()
  4. None
Qus. 47 : Which keyword is used to remove individual items or the entire dictionary itself.

  1. del
  2. remove
  3. removeAll
  4. None of these
Qus. 48 : Suppose d = {"john”:40, “peter":45}. To obtain the number of entries in dictionary which command do we use?

  1. d.size()
  2. len(d)
  3. size(d)
  4. d.len ()
Qus. 49 : Which of the following is a mapping datatype?

  1. String
  2. List
  3. Tuple
  4. Dictionary
Qus. 50 : Which one is not a built in function

  1. dictionary()
  2. set()
  3. tuple()
  4. list()
Qus. 51 : Dictionary has:

  1. Sequence value pair
  2. Key value pair
  3. Tuple value pair
  4. Record value pair

Programs

python program to find the 2nd largest number from the list of the numbers entered through keyboard.

View Solution


python program that creates a list of numbers from 1 to 20 that are divisible by 4

View Solution


python program to define a list of countries that are a member of BRICS Check whether a county is member of BRICS or not

View Solution


Python program to read marks of six subjects and to print the marks scored in each subject and show the total marks

View Solution


Python program to read prices of 5 items in a list and then display sum of all the prices product of all the prices and find the average

View Solution


Python program to count the number of employees earning more than 1 lakh per annum

View Solution


python program to create a list of numbers in the range 1 to 10 Then delete all the even numbers from the list and print the final list

View Solution


python program to generate in the Fibonacci series and store it in a list Then find the sum of all values

View Solution


python program to swap two values using tuple assignment

View Solution


python program using a function that returns the area and circumference of a circle whose radius is passed as an argument

View Solution


python program that has a list of positive and negative numbers Create a new tuple that has only positive numbers from the list

View Solution


Python Program that generate a set of prime numbers and another set of even numbers

View Solution


python program to find minimum element from a list of elements along with its index in the list

View Solution


Python program to calculate mean of a given list of numbers

View Solution


Python program to search for an element in a given list of numbers

View Solution


python program to count frequency of a given element in a list of numbers

View Solution


python program to calculate the sum of integers of the list

View Solution


python program to reverses an array of integers

View Solution


python program to creates a third list after adding two lists

View Solution


python program to store the product information in dictionary

View Solution


python program to create a dictionary whose keys are month names and values are their corresponding number of days

View Solution


python program that input a list, replace it twice and then prints the sorted list in ascending and descending order

View Solution


python program to check if the maximum element of the list lies in the first half of the list or in the second half

View Solution


python program to display the maximum elements from the two list along with its index in its list

View Solution


python program to swap list elements with even and locations

View Solution


python program to rotate list elements with next elements

View Solution


python program to find maximum and minimum values in tuple

View Solution


python program to interchange the values of two tuple

View Solution


python program to interchange the values of two tuple

View Solution


python program to increment the elements of list

View Solution


python program to enter a list containing number between 1 and 12 then replace all the entries in the list that are greater than 10 with 10

View Solution


python program that reverse the list of integers in place

View Solution


python program to input two list and create third list which contains all elements of the first elements followed by all elements of second list

View Solution


python program to inter list of string and create a new list that consist of those strings with their first characters removed

View Solution


python program to create a list using for loop which consist of integers 0 through 49

View Solution


python program to create a list using for loop which consist square of the integers 1 through 50

View Solution


python program to create a list using for loop which consist a bb ccc dddd that ends with 26 copies of the letter z.

View Solution


python program that takes any two list L and M of the same size and adds their elements together in another third list

View Solution


python program to rotates the elements of list so that fist elements move to second and second to the third

View Solution


python program to move all duplicate element to the end of list

View Solution


python program to display the maximum and minimum values form specified range of indexes of a list

View Solution


python program to compare two equal sized list and print the first index where they differ

View Solution


python program to enter names of employees and their salaries as input and store them in a dictionary

View Solution


python program to count the number of times a character appears in a given string

View Solution


python program to convert a number entered by the user into its corresponding number in words

View Solution


python program Repeatedly ask the user to enter a team name

View Solution


python program that repeatedly asks the user to enter product names and prices

View Solution


Python Program to Create a dictionary whose keys are month name and whose values are number of days in the corresponding month

View Solution


python program to store the detail of 10 students in a dictionary at the same time

View Solution


python program to Create a dictionary with the opposite mapping

View Solution


python program that lists the over lapping keys of the two dictionaries if a key of d1 is also a key of d2 the list it

View Solution


python program that checks if two same values in a dictionary have different keys

View Solution


python program to check if a dictionary is contained in another dictionary

View Solution


python program to create a new dictionary D2 having same keys as D1 but values as the sum of the list elements

View Solution


python program to create a dictionary has three keys assets liabilities and capital

View Solution


python program that create a tuple storing first 9 terms of Fibonacci series

View Solution


python program that receives the index and return the corresponding value

View Solution


Python program that receives a Fibonacci series term and returns a number telling which term it is

View Solution


python program to create a nested tuple to store roll number name and marks of students

View Solution


python program that interactively creates a nested tuple to store the marks in three subjects for five students

View Solution


Python Program to accept three distinct digits and print all possible combinations from the digits

View Solution


Python Program to find the successor and predecessor of the largest element in list

View Solution


Python function that takes a string as parameter and returns a string with every successive repetitive character replaced

View Solution


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad