Reputation: 5
So lets say my list is
Colors=["red", "blue", "green", "purple"]
Then, I want to assign red to [1,2,3,4]
.
For example: red=[1,2,3,4]
How do you take an item in a list and assign something to it?
Clarification: without just changing the Colors list and by creating a whole new variable red.
Upvotes: 1
Views: 62
Reputation: 19274
I would suggest using a dict
:
>>> myobj = {}
>>> for i in ["red", "blue", "green", "purple"]:
... myobj[i] = []
...
>>> myobj['red'] = [1, 2, 3, 4]
>>> myobj['red']
[1, 2, 3, 4]
>>> myobj.keys()
['blue', 'purple', 'green', 'red']
As shown above, you can use dict.keys()
to get the keys (colors, in this example) of the dictionary.
Upvotes: 5