⊗pyPmDcCTD 104 of 208 menu

Converting to Dictionary in Python

To convert any object to a dictionary, you need to apply the dict function. However, not all data types can become a dictionary. Let's try to convert a string and a list to a dictionary:

txt = '12345' dct = dict(txt) # error lst = ['1', '2', '3', '4', '5'] dct = dict(lst) # error

This is because the object must have paired values. Now let's create a dictionary from nested lists:

lst = [['a', '1'], ['b', '2']] dct = dict(lst) print(dct) # {'a': '1', 'b': '2'}

Nested tuples can also be converted into a dictionary:

tlp = ((1, 'a'), (2, 'b')) dct = dict(tlp) print(dct) # {1: 'a', 2: 'b'}

The following code is given:

tst = [[1, 'ab'], [2, 'cd'], [3, 'ef']] dct = dict(tst) print(dct)

Tell me what will be output to the console.

The following code is given:

tst = [('x', 2), ('y', 4), ('z', 6)] dct = dict(tst) print(dct)

Tell me what will be output to the console.

The following code is given:

tst = ['a', 'b', 'c', 'd'] dct = dict(tst) print(dct)

Tell me what will be output to the console.

The following code is given:

tst = ('a', 1), ('b', 2), ('c', 3) dct = dict(tst) print(dct)

Tell me what will be output to the console.

plithuazcs