newbie94
newbie94

Reputation: 63

How can I add multiple lists to a key?

I have this:

{'Sleep': ['Bed', 'Sheets', 'Warm', 'TV', 'Computer', 'Phone']}

But I want this:

{'Sleep': [['Bed', 'Sheets', 'Warm'], ['TV', 'Computer', 'Phone']]}

I've tried using:

def func():
    for element in data:
        if element[1] in b:
            b[element[1]].append([element[0], element[2], element[3], element[4], element[5]]) 
        else:
            b[element[1]] = [element[0], element[2], element[3], element[4], element[5]]

I've also tried .extend() and b[element[1] +=, in place of .append() But it's returning for append:

{'Sleep': ['Bed', 'Sheets', 'Warm', ['TV', 'Computer', 'Phone']]}

And returning in +=:

{'Sleep': ['Bed', 'Sheets', 'Warm', 'TV', 'Computer', 'Phone']}

I understand why they are returning that way but I don't know how to keep the lists separate. So, my question is how can I keep the lists separate?

Upvotes: 1

Views: 197

Answers (3)

Eugene V
Eugene V

Reputation: 3126

First, you need to create an empty list as a dictionary value:

d = {'Sleep': []}

Then, add lists, which you want it to contain:

item_one = ['Bed', 'Sheets', 'Warm']
d['Sleep'].append(item_one)

item_two = ['TV', 'Computer', 'Phone']
d['Sleep'].append(item_two)

Wrapped into function:

def add_list_to_dict(to_dict, key, item):
    if key not in to_dict.keys():
        to_dict[key] = []
    to_dict[key].append(item)

# usage:
add_list_to_dict(d, 'Sleep', item_one)
add_list_to_dict(d, 'Sleep', item_two)

Or like this, using args:

def add_list_to_dict(to_dict, key, *items):
    if key not in to_dict.keys():
        to_dict[key] = []
    for item in items:
        to_dict[key].append(item)

# usage:
add_list_to_dict(d, 'Sleep', item_one, item_two)

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1124170

You can't have more than one value per key, no.

You can create a new list containing your existing lists, and make that the value:

def func():
    for element in data:
        if element[1] in b:
            b[element[1]].append([element[0], element[2], element[3], element[4], element[5]]) 
        else:
            b[element[1]] = [[element[0], element[2], element[3], element[4], element[5]]]

Note that when the key is not present, we add a nested list.

This now produces:

{'Sleep': [['Bed', 'Sheets', 'Warm'], ['TV', 'Computer', 'Phone']]}

You could use the dict.setdefault() method rather than testing for containment here, and perhaps some slicing to build a new list:

def func():
    for element in data:
        b.setdefault(element[1], []).append([element[:1] + element[2:]) 

dict.setdefault() either returns the value for the given key, or if the key is missing, adds the second argument as the value for that key, then returns the value.

Upvotes: 1

ArslanW
ArslanW

Reputation: 353

If you are using the same key, then keeping the lists separate is not possible. Dictionaries go with key value (wholesome value) pairs. Having one key point to two different arrays is not possible. What can be done is have a dictionary within a dictionary like:

{ "key1": { "0": [1, 2...], "1": [44, 55...] } }

Upvotes: 0

Related Questions