Informatics Practices

Suppose

>>> d1 = { 1 : 'one' , 2: 'two' , 3: 'three' , 4: 'four'}
>>> d2 = { 5 :'five', 6:'six' }

Write the output of the following code:

>>> d1.items() 
>>> d1.keys() 
>>> d1.values() 
>>> d1.update(d2)
>>> len(d1)

Python Dictionaries

5 Likes

Answer

dict_items([(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')])
dict_keys([1, 2, 3, 4])
dict_values(['one', 'two', 'three', 'four'])
6

Working

  1. d1.items() — Returns a list of tuples containing the key-value pairs in d1.
  2. d1.keys() — Returns a list of all the keys in d1.
  3. d1.values() — Returns a list of all the values in d1.
  4. d1.update(d2) — Updates d1 with key-value pairs from d2. After this operation, d1 contains {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}.
  5. len(d1) — Returns the number of key-value pairs in d1, which is 6 after updating.

Answered By

1 Like


Related Questions