Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting list of lists to a dictionary with multiple values for a key

I need to write a function that accepts a list of lists representing friends for each person and need to convert it into a dictionary. so an input of [['A','B'],['A','C'],['A','D'],['B','A'],['C','B'],['C','D'],['D','B'],['E']] should return {A:[B,C,D],B:[A],C:[B,D],D:[B],E:None}

Input:

[['A','B'],['A','C'],['A','D'],['B','A'],['C','B'],['C','D'],['D','B'],['E']]

Expected Output:

{A:[B,C,D],B:[A],C:[B,D],D:[B],E:None}

Currently I am trying the following:

s=[['A','B'],['A','C'],['A','D'],['B','A'],['C','B'],['C','D'],['D','B'],['E']]

output=dict.fromkeys((set([x[0] for x in s])),[ ])

for x in s:
    if len(x)>1:
        output[x[0]].append(x[1])
    else:
        output[x[0]].append(None)

But the output is giving me all values for every key rather than returning only the corresponding values

The output i am getting is:

{
'A': ['B', 'C', 'D', 'A', 'B', 'D', 'B', None],

 'B': ['B', 'C', 'D', 'A', 'B', 'D', 'B', None],

 'C': ['B', 'C', 'D', 'A', 'B', 'D', 'B', None],

 'D': ['B', 'C', 'D', 'A', 'B', 'D', 'B', None],

 'E': ['B', 'C', 'D', 'A', 'B', 'D', 'B', None]
}
like image 524
Sahil Katiyar Avatar asked Dec 03 '18 22:12

Sahil Katiyar


People also ask

How do you create a dictionary with multiple values per key?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

How do I convert a list to a dictionary key?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.

How do I convert a nested list to a dictionary?

We can convert a nested list to a dictionary by using dictionary comprehension. It will iterate through the list. It will take the item at index 0 as key and index 1 as value.

Can you convert a list into a dictionary?

To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.


1 Answers

You can iterate through the key-value pairs in the list of lists, but unpack the value as a list to accommodate the possible lack of a value:

s = [['A','B'],['A','C'],['A','D'],['B','A'],['C','B'],['C','D'],['D','B'],['E']]
output = {}
for k, *v in s:
    if v:
        output.setdefault(k, []).extend(v)
    else:
        output[k] = None

output becomes:

{'A': ['B', 'C', 'D'], 'B': ['A'], 'C': ['B', 'D'], 'D': ['B'], 'E': None}

Or if you don't mind that keys without a value get an empty list instead of None, you can simply do:

output = {}
for k, *v in s:
    output.setdefault(k, []).extend(v)

output would then become:

{'A': ['B', 'C', 'D'], 'B': ['A'], 'C': ['B', 'D'], 'D': ['B'], 'E': []}
like image 98
blhsing Avatar answered Oct 24 '22 07:10

blhsing