S
S
sswwssww2019-12-10 13:51:30
Data types
sswwssww, 2019-12-10 13:51:30

How to create a dictionary using dict?

I have two variables: x = 'one', y = 1
I want to create a dictionary with key x and value y using the dict class. Other methods are of no interest.
Logically, I write the following:

x, y = 'one', 1
my_dict = dict(x, y)

Writes an error: "TypeError: dict expected at most 1 arguments, got 2"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sebastian Lore, 2019-12-10
@sswwssww

dict() expects an Iterable, i.e. something like this should work:

x, y = 'one', 1
my_dict = dict([(x, y),])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question