The following code has two dictionaries with tuples as keys. While one of these dictionaries being
successfully created, the other is giving some error. Find out which dictionary will be created
successfully and which one will give error and correct it :
dict1 = {(1, 2): [1, 2], (3, 4): [3, 4]}
dict2 = { ([1], [2]): [1,2], ([3], [4]): [3, 4]}
Answers
Answer:
We have already become acquainted with lists in the previous chapter. In this chapter of our online Python course we will present the dictionaries, the operators and the methods on dictionaries. Python programs or scripts without lists and dictionaries are nearly inconceivable. Dictionaries and their powerful implementations are part of what makes Python so effective and superior. Like lists, they can be easily changed, can be shrunk and grown ad libitum at run time. They shrink and grow without the necessity of making copies. Dictionaries can be contained in lists and vice versa.
Answer:
[] : Used to define mutable data types
So dict 1 will be created as brackets for keys are of immutable type. For dict 2, round brackets should be replaced by square brackets as in 1. So,
dict2={ (1, 2): [1,2], (3, 4): [3, 4]}
Explanation:
Hope this helps!