izzyp
izzyp

Reputation: 173

Having two lists as a value to a single key: Dictionaries

Looking for a way to add two lists to a value in a dictionary for example if want to add two lists to a single value for example

For example this dictionary dict1 = {a: [], b:[], c:[]}

Would become dict1 = {a:([1,3,4,5,6],[1,2,3,4]),b:[],c:[]}

When you add the two lists [1,2,3,4,5,6] and [1,2,3,4] to the key a. And if you were to print the key a you would get the two lists as output

Any help would be much appreciated

Upvotes: 0

Views: 53

Answers (2)

R.A.Munna
R.A.Munna

Reputation: 1709

you can also doing it in this way by adding two list [a]+[b]

Edit: from the comment. Another better way [a,b]

dict1 = {'a': [], 'b':[], 'c':[]}
a = [1,2,3,4,5,6]
b = ['a','b']
dict1['a']=[a]+[b]
print(dict1)

Upvotes: 1

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

You can use list.append() because your dict1['a']'s value is a list. See this example:

dict1 = {'a': [], 'b':[], 'c':[]}

a = [1,2,3,4,5,6]
b = [1,2,3,4]

dict1['a'].append(a)
dict1['a'].append(b)

print(dict1)

Output:

{'a': [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4]], 'b': [], 'c': []}

Upvotes: 3

Related Questions