An Alternative Way to Create a Dictionary in Python
An alternative way to create dictionaries is to use the dict function. You need to pass a key-value pair to its parameters. If you do not specify anything in the parameters, the created dictionary will be empty:
dct = dict()
print(dct) # {}
Now let's create a dictionary with two elements. To do this, we pass a string key to the first parameter of the function, but without quotes. After it, we put the operator = and write the value. Separated by a comma, we specify the second pair:
dct = dict(a='1', b='2')
print(dct) # {'a': '1', 'b': '2'}
However, you cannot pass numbers as keys to the dict functions. In this case, an error will be returned:
dct = dict(1='a', 2='b') # will display an error
The following code is given:
dct = dict(a=1, b=2, c=3)
print(dct)
Tell me what will be output to the console.
The following code is given:
dict('1'='a', '2'='b', '3'='c')
print(dct)
Tell me what will be output to the console.
The following code is given:
dict(a='12', b='34', c='56')
print(dct)
Tell me what will be output to the console.
The following code is given:
dct = dict(0='abc', 1='def')
print(dct)
Tell me what will be output to the console.