Home > other >  Adding list as dictionary value
Adding list as dictionary value

Time:01-08

if I have a list of list, for ex

l=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

and a dictionary whose value is of dictionary type itself, for ex

d={"id1":{"A": "", "C": "", "G": "", "T": ""},"id2":{"A":"","C":"","G":"","T":""},
"id3":{"A": "", "C": "", "G": "", "T": ""},"id4":{"A": "", "C": "", "G": "", "T": ""}}

I want loop through the list and each item to the corresponding key in d, output would be like

d={"id1":{"A": "1", "C": "2", "G": "3", "T": "4"},"id2":{"A":"5","C":"6","G":"7","T":"8"},
"id3":{"A": "9", "C": "10", "G": "11", "T": "12"},"id4":{"A": "13", "C": "14", "G": "15", "T": "16"}}

but instead it gives me the following

{'id1': {'A': 15, 'C': 15, 'G': 15, 'T': 15}, 'id2': {'A': 15, 'C': 15, 'G': 15, 'T': 15}, 'id3': {'A': 15, 'C': 15, 'G': 15, 'T': 15}, 'id4': {'A': 15, 'C': 15, 'G': 15, 'T': 15}}

my code so far

for k,v in d.items():
    for sub_k,sub_value in v.items():
        for i in range(len(l)):
            for j in range(len(l[i])):
                d[k][sub_k]=l[i][j]

CodePudding user response:

The issue with your code is that you iterate twice on each level of dict, and so, the results you see are the ones of the last iteration. You can fix your code by doing:

for i, (k,v) in enumerate(d.items()):
    for j, (sub_k,sub_value) in enumerate(v.items()):
         d[k][sub_k]=str(l[i][j]) # adding str() allows to change the int to a str

enumerate allows you to iterate on your dict (k, v for instance) and to have access to the index (i for instance)

CodePudding user response:

This is my idea:

new_dict = list(zip(d.keys(), d.values(), l))
updated_dic = {}
for i in new_dict:
    updated_dic[i[0]] = (dict(zip(i[1], i[2])))

output:

{'id1': {'A': 1, 'C': 2, 'G': 3, 'T': 4}, 'id2': {'A': 5, 'C': 6, 'G': 7, 'T': 8}, 'id3': {'A': 9, 'C': 10, 'G': 11, 'T': 12}, 'id4': {'A': 13, 'C': 14, 'G': 15, 'T': 16}}

CodePudding user response:

Since the lengths match, instead of using a double for-loop, you can zip d with l to create tuples that you iterate only once. Use the dict constructor on zipped iterables (of inner dictionary keys and sublists) to construct the inner dictionaries.

for (Id, dct), vals in zip(d.items(), l):
    d[Id] = dict(zip(dct, vals))

This can be written more concisely as a dict comprehension:

d = {Id: dict(zip(dct, vals)) for (Id, dct), vals in zip(d.items(), l)}

Output:

{'id1': {'A': 1, 'C': 2, 'G': 3, 'T': 4},
 'id2': {'A': 5, 'C': 6, 'G': 7, 'T': 8},
 'id3': {'A': 9, 'C': 10, 'G': 11, 'T': 12},
 'id4': {'A': 13, 'C': 14, 'G': 15, 'T': 16}}

CodePudding user response:

This will do it for you

l=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
d={"id1":{"A": "", "C": "", "G": "", "T": ""},"id2":{"A":"","C":"","G":"","T":""},
"id3":{"A": "", "C": "", "G": "", "T": ""},"id4":{"A": "", "C": "", "G": "", "T": ""}}

dl = [inner.keys() for inner in d.values()] # get a list of lists of the keys
print({f"id{i 1}": dict(zip(*p)) for i, p in enumerate(zip(dl, l))}) 
# first zip the sublists of values with the sublist of keys
# then zip the tuple of sublists of values and sublist of keys by unpacking it using * 
# we get something like (("a", 1), ("b", 2)) which can be converted to a dict
# finally use dict comprehension to puth the id's back

But it is fairly hard to read, so comments will be needed

CodePudding user response:

Whole code should look like this:

d = {"id1":{"A": "", "C": "", "G": "", "T": ""},"id2":{"A":"","C":"","G":"","T":""},
"id3":{"A": "", "C": "", "G": "", "T": ""},"id4":{"A": "", "C": "", "G": "", "T": ""}}
l = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

for i, (k, v) in enumerate(d.items()):
    for j, (k2, value) in enumerate(v.items()):
         d[k][k2]=str(l[i][j])

print (d)

It solves both the string formatting and the values being put inside the dictionary

CodePudding user response:

l = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
d = {"id1": {"A": "", "C": "", "G": "", "T": ""}, "id2": {"A": "", "C": "", "G": "", "T": ""},
     "id3": {"A": "", "C": "", "G": "", "T": ""}, "id4": {"A": "", "C": "", "G": "", "T": ""}}

for index, key in enumerate(d.keys()):
    for new_key, val in zip(d[key].keys(), l[index]):
        d[key][new_key] = str(val)

print(d)
  •  Tags:  
  • Related