user3813114
user3813114

Reputation: 1

Creating a dictionary from list

Right now I have a list of words and numbers:

List = [("purple", 8), ("orange", 2),...]

I want to make this into a dictionary:

Dictionary = [{"word":" purple", "number":8}, ...]

How do I go about this? Thanks in advance for any help

Upvotes: 0

Views: 66

Answers (2)

vaultah
vaultah

Reputation: 46533

In [3]: [{'word': x, 'number': y} for x, y in List]
Out[3]: [{'word': 'purple', 'number': 8}, {'word': 'orange', 'number': 2}]

Though most probably you want this:

In [4]: dict(List)
Out[4]: {'orange': 2, 'purple': 8}

Upvotes: 4

Jamie Cockburn
Jamie Cockburn

Reputation: 7555

Dictionary = [{"word": word, "number": number} for word, number in List]

Upvotes: 1

Related Questions