user3197309
user3197309

Reputation: 95

how to insert two lists as values to a dictionary to the same key

Suppose mydict = {'key1': ['value1', 'value2'],'key2': ['value1', 'value2']}

Here I want to append or add another list (Say ['NewValue1', 'NewValue2']) to the Entity with key "key1" such that it will become like

mydict = {'key1': ['value1', 'value2'],['NewValue1', 'NewValue2'] ,'key2': ['value1', 'value2']}

And in future I want to access all the lists under the key "key1".

Upvotes: 2

Views: 70

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250931

Convert the lists in the dictionary to list of lists.

>>> mydict = {'key1': [['value1', 'value2']],'key2': [['value1', 'value2']]}

Now you can easily append a new list to any of the key and later access individual lists under a key using either a for-loop or indexing.

>>> mydict['key1'].append(['NewValue1', 'NewValue2'])
>>> mydict
{'key2': [['value1', 'value2']],
 'key1': [['value1', 'value2'], ['NewValue1', 'NewValue2']]}

Upvotes: 5

Related Questions