I am trying to add the elements of a list of list to the values of a dictionary.
I have created a list with elements from a file that looks like this:
list_of_lists = [[966], [1513, 2410], [2964, 1520, 604]....]
I am trying to add this list to a dictionary that I have made to look like this:
{'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011], 'Yakima': ['WA', 4660, 12051, 49826]....]
I have tried the following code:
x = 1
for x in d2.values():
d2.append(list_of_list)
print(d2)
I am not even sure that this is something that is possible, but I am trying to get the dictionary to be:
{'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011, [966]], 'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]....]
CodePudding user response:
I know there are more ways to do that, But I think this is more readable and understandable code.
list_of_lists = [[966], [1513, 2410], [2964, 1520, 604]]
dict_ = {'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011], 'Yakima': ['WA', 4660, 12051, 49826]}
i = 0
# list(dict_.items())[1:] is the list of all keys and values except first one.
for key,value in list(dict_.items())[1:]:
dict_[key] = value [list_of_lists[i]]
i =1
print(dict_)
CodePudding user response:
You can use itertools.islice()
to skip the first element, and then use zip()
to pair each list value with the list to append:
from itertools import islice
for lst_value, lst_to_append in zip(islice(d2.values(), 1, None), list_of_lists):
lst_value.append(lst_to_append)
print(d2)
This outputs:
{
'Youngstown': ['OH', 4110, 8065, 115436],
'Yankton': ['SD', 4288, 9739, 12011, [966]],
'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]
}
CodePudding user response:
You do x = 1
, and then immediately you do for x in d2.values()
. This overwrites x
with each element of d2.values()
. If you want to start from the second item in d2.values()
, you need to do create an iterator and skip the first value:
d2_iter = iter(d2.values())
next(d2_iter) # Consume one element
for item in d2_iter: # Iterate over the remaining iterator
# Do what you want here.
Another problem is that you append the entire list-of-lists to every value in d2
. Don't do that. Instead use zip()
to iterate over the list-of-lists and the values in d2
simultaneously
d2_iter = iter(d2.values())
next(d2_iter) # Consume one element
for item_from_dict, item_to_append in zip(d2_iter, list_of_lists):
item_from_dict.append(item_to_append)
which leaves you with:
{'Youngstown': ['OH', 4110, 8065, 115436],
'Yankton': ['SD', 4288, 9739, 12011, [966]],
'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]}
Note that appending like this only works because lists are mutable. If you had an immutable type like tuples as the values of d2
, you'd have to create a new tuple and assign it to the key:
d3 = {'Youngstown': ('OH', 4110, 8065, 115436), 'Yankton': ('SD', 4288, 9739, 12011), 'Yakima': ('WA', 4660, 12051, 49826)}
d3_iter = iter(d2.keys())
next(d3_iter) # Consume one element
for key_from_dict, item_to_append in zip(d3_iter, list_of_lists):
new_item = d3[key_from_dict] (item_to_append,) # Create a new item
d3[key_from_dict] = new_item
and you'd get
{'Youngstown': ('OH', 4110, 8065, 115436),
'Yankton': ('SD', 4288, 9739, 12011, [966]),
'Yakima': ('WA', 4660, 12051, 49826, [1513, 2410])}