Computer Science
Which of the following Python codes will give the same output if
dict = {"diary":1, "book":3, "novel":5}
(i) dict.pop("book")
(ii) del dict["book"]
(iii) dict.update({"diary":1,"novel":5})
- (i), (ii), (iii)
- (1), (ii)
- (i), (iii)
- (ii), (iii)
Python Dictionaries
4 Likes
Answer
(i), (ii)
Reason — Expression dict.pop("book") and del dict["book"] will give same output as both of them are removing the key "book" from dict i.e., {'diary': 1, 'novel': 5}
Answered By
2 Likes
Related Questions
Which of the following will delete key_value pair for key="tiger" in dictionary?
di = {"lion":"wild","tiger":"wild","cat": "domestic", "dog":"domestic"}- del di["tiger"]
- di["tiger"].delete( )
- delete(di["tiger"])
- del(di.["tiger"])
Which of the following will give error if d1 is as shown below?
d1 = {"a":1, "b":2, "c":3}- print(len(d1))
- print(d1.get("b"))
- d1["a"] = 5
- None of these
What will be the output of following Python code?
d1 = {"a":10,"b":2,"c":3} str1="" for i in d1: str1 = str1 + str(d1[i]) + " " str2 = str1[:-1] print(str2[::-1])- 3, 2
- 3, 2, 10
- 3, 2, 01
- Error
Running the code sorted(mydictionary, reverse = True) on a dictionary named mydictionary will return results sorted in what order?
- Ascending order (A-Z), by key
- Ascending order (A-Z), by value
- Descending order (Z-A), by key
- Descending order (Z-A), by value