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 : Which of the following statements create a dictionary?
- d = {}
- d = {“john”:40, “peter”:45}
- d = {40:”john”, 45:”peter”}
- All of the mentioned
Qus. 2 :
What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
- “john”, 40, 45, and “peter”
- “john” and “peter”
- 40 and 45
- d = (40:”john”, 45:”peter”)
Qus. 3 :
What will be the output of the following Python code snippet?
-
d = {"john":40, "peter":45}
-
"john" in d
- True
- False
- None
- Error
Qus. 4 :
What will be the output of the following Python code snippet?
-
d1 = {"john":40, "peter":45}
-
d2 = {"john":466, "peter":45}
-
d1 == d2
- True
- False
- None
- Error
Qus. 5 :
What will be the output of the following Python code snippet?
-
d1 = {"john":40, "peter":45}
-
d2 = {"john":466, "peter":45}
-
d1 > d2
- True
- False
- Error
- None
Qus. 6 :
What will be the output of the following Python code snippet?
-
d = {"john":40, "peter":45}
-
d["john"]
- 40
- 45
- “john”
- “peter”
Qus. 7 : 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. 8 : 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. 9 :
What will be the output of the following Python code snippet?
-
d = {"john":40, "peter":45}
-
print(list(d.keys()))
- [“john”, “peter”]
- [“john”:40, “peter”:45]
- (“john”, “peter”)
- (“john”:40, “peter”:45)
Qus. 10 : 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. 11 : 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. 12 : 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. 13 :
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
- 1 A 2 B 3 C
- 1 2 3
- A B C
- 1:”A” 2:”B” 3:”C”
Qus. 14 :
What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
- 1
- A
- 4
- Invalid syntax for get method
Qus. 15 : <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. 16 : <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. 17 : <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. 18 : <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. 19 : <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. 20 : 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. 21 : <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. 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">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. 23 : <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. 24 : 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. 25 : <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. 26 : 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. 27 : <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. 28 : <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. 29 : <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. 30 : <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. 31 : <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. 32 : <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. 33 : <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. 34 : <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. 35 : <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. 36 : <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. 37 : 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. 38 : <p>What will be the output of the following Python code?</p> <pre>a=dict()<br>a[1]</pre><div><br></div>
- An exception is thrown since the dictionary is empty
- ‘ ‘
- 1
- 0
Qus. 39 : <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. 40 : In which of the following data type, duplicate items are not allowed ?
- List
- Dictionary
- Set
- None of the Above
Qus. 41 : <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. 42 : <p>What will be the output of the following?</p><pre>print(sum(1,2,3))</pre>
- error
- 6
- 1
- 3
Qus. 43 : <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. 44 : <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. 45 : Which of the following function of dictionary gets all the keys from the dictionary?
- getkeys ()
- key()
- keys()
- None
Qus. 46 : Which keyword is used to remove individual items or the entire dictionary itself.
- del
- remove
- removeAll
- None of these
Qus. 47 : 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. 48 : Which of the following is a mapping datatype?
- String
- List
- Tuple
- Dictionary
Qus. 49 : Which one is not a built in function
- dictionary()
- set()
- tuple()
- list()
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 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 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