Transform multiple lists to dictionary of tuples [duplicate] - python

This question already has answers here:
How to map multiple lists to one dictionary?
(2 answers)
Closed 2 years ago.
I'm attempting to convert multiple lists into a dictionary where the initial list contains the keys.
For example:
list_keys = ['a' , 'b']
list_vals_1 = ['tick' , 'tack']
list_vals_2 = ['big' , 'small']
is transformed into:
expected = {}
expected['a'] = ('tick' , 'big')
expected['b'] = ('tack', 'small')
print('expected' , expected)
I could transform the lists using into a dictionary using:
mappings = {}
i = 0
for l in list_keys :
mappings[l] = (list_vals_1[i] , list_vals_2[i])
i = i + 1
Is there a cleaner/better solution using Python to accomplish this ?

try zip
dict(zip(list_keys, zip(list_vals_1, list_vals_2)))
{'a': ('tick', 'big'), 'b': ('tack', 'small')}

Use zip():
>>> {k: (v1, v2) for k, v1, v2 in zip(list_keys, list_vals_1, list_vals_2)}
{'a': ('tick', 'big'), 'b': ('tack', 'small')}
Also, for what it's worth, you could have used enumerate() to improve your existing solution:
for i, k in enumerate(list_keys):
mappings[k] = (list_vals_1[i], list_vals_2[i])
By the way, note that l is a bad variable name since it looks like 1 and I. I used k instead - short for "key".

Related

Convert two lists to a dictionary but Sum the values for Same Keys [duplicate]

This question already has an answer here:
How to merge two lists into a dictionary and sum values for duplicate keys [duplicate]
(1 answer)
Closed 1 year ago.
This is my first time asking a question on stack overflow myself but have been using the resources here for quite a while - thanks!
list_1 = ['a','b','c','c']
list_2 = [2,2,2,2]
I want to combine both the lists and get add values of the same keys
my_dict = dict(zip(stream_ids, times))
but whenever I combine the lists to dictionaries - value for the key gets replaced by the last one
Output: {'a': 2, 'b': 2, 'c': 2}
I want both 'c' to be added to get a 4
What I want to get is
{'a': 2, 'b': 2, 'c': 4}
Any help would be really appreciated!
The most readable way would be to use a simple for loop:
stream_ids = ['a','b','c','c']
times = [2,2,2,2]
my_dict = {}
for idx, count in zip(stream_ids, times):
if idx in dict:
my_dict[idx] += count
else:
my_dict[idx] = count
You can probably do it in one line too using a lamda function and dictionary comprehension but that won't be very readable.
A dict will simply overwrite existing values. You have to build your dict by your own.
stream_ids = ['a','b','c','c']
times = [2,2,2,2]
my_dict = {}
for s, t in zip(stream_ids, times):
my_dict[s] = my_dict.get(s, 0) + t
print(my_dict) # {'a': 2, 'b': 2, 'c': 4}
A variant of other answers here, with collections.defaultdict:
from collections import defaultdict
stream_ids = ['a','b','c','c']
times = [2,2,2,2]
dict = defaultdict(int)
for idx, count in zip(stream_ids, times):
dict[idx] += count
since the default for a new integer is 0.

Mapping array to sequential numbers via dictionary [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 4 years ago.
I have an array:
classes = ["banana","mango","apple"]
I am trying to print this array in a specific format where in each element has to be numbered in a particular sequence. The desired output is as follows:
classes = [{"banana" : 1, "mango" : 2, "apple" : 3}]
I tried using a for loop as follows:
classes = ["banana","mango","apple"]
counter = 0
dat = []
for x in classes:
counter=counter+1
d = x,":", counter
dat.append(d)
print(dat)
While this prints
[('banana', ':', 1), ('mango', ':', 2), ('apple', ':', 3)]
this is far from what I require. Can someone help?
You can enumerate the input list and reverse via a dictionary comprehension.
classes = ["banana","mango","apple"]
res = {v: k for k, v in enumerate(classes, 1)}
# {'apple': 3, 'banana': 1, 'mango': 2}
There seems to be no need to put this dictionary in a list, as in your desired output.

python dictionary updating/appending values for a key as a list [duplicate]

This question already has answers here:
Initialize List to a variable in a Dictionary inside a loop
(2 answers)
Closed 5 years ago.
I have a dictionary that needs to create a key when the key first shows up and adds a value for it, later on, keeps updating the key with values by appending these values to the previous value(s), I am wondering how to do that.
outter_dict = defaultdict(dict)
num_index = 100
outter_dict['A'].update({num_index: 1})
outter_dict['A'].update({num_index: 2})
2 will replace 1 as the value for key 100 of the inner dict of outter_dict, but ideally, it should look like,
'A': {100:[1,2]}
UPDATE
outter_dict = defaultdict(list)
outter_dict['A'][1].append(2)
but I got
IndexError: list index out of range
if I do
dict['A'][1] = list()
before assign any values to 1, I got
IndexError: list assignment index out of range
You can use a collections.defaultdict:
from collections import defaultdict
d = defaultdict(list)
num_index = 100
d[num_index].append(1)
d[num_index].append(2)
print(dict(d))
Output:
{100: [1, 2]}
Regarding your most recent edit, you want to use defautldict(dict) and setdefault:
outter_dict = defaultdict(dict)
outter_dict["A"].setdefault(1, []).append(2)
print(dict(outter_dict))
Output:
{'A': {1: [2]}}

How to take several items from dictionary [duplicate]

This question already has answers here:
Extract a subset of key-value pairs from dictionary?
(14 answers)
Filter dict to contain only certain keys?
(22 answers)
Closed 5 years ago.
Dictionary:
d = {'a':[2,3,4,5],
'b':[1,2,3,4],
'c':[5,6,7,8],
'd':[4,2,7,1]}
I want to have d_new which containes only b and c items.
d_new = {'b':[1,2,3,4],
'c':[5,6,7,8]}
I want a scalable solution
EDIT:
I also need a method to create a new dictionary by numbers of items:
d_new_from_0_to_2 = {'a':[2,3,4,5],
'b':[1,2,3,4]}
If you want a general way to pick particular keys (and their values) from a dict, you can do something like this:
d = {'a':[2,3,4,5],
'b':[1,2,3,4],
'c':[5,6,7,8],
'd':[4,2,7,1]}
selected_keys = ['a','b']
new_d = { k: d[k] for k in selected_keys }
Gives:
{'a': [2, 3, 4, 5], 'b': [1, 2, 3, 4]}
I think that in Python 2.6 and earlier you would not be able to use a dict comprehension, so you would have to use:
new_d = dict((k,d[k]) for k in selected_keys)
Is this what you want?
new_d = dict(b=d.get('b'), c=d.get('c'))

How to merge dictionaries in a list based on a value in the dictionary in Python? [duplicate]

This question already has answers here:
Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?
(22 answers)
Closed 8 years ago.
I have a list of dictionaries as shown below:
result = [
{'so': 'SO1', 'amt':250},
{'so': 'SO2', 'amt':200},
{'so': 'SO1', 'amt':100}
]
I need to merge it so that the result would be:
result = [
{'so': 'SO1', 'amt':350},
{'so': 'SO2', 'amt':200}
]
that is the dictionaries with same SO are merged and their amt is added.
Collections.counter provides an elegant way to merge dictionaries this way.
from collections import Counter
counters = [Counter({k['so']: k['amt']}) for k in result]
r = sum(counters, Counter())
# Counter({'SO1': 350, 'SO2': 200})
result = [{'so': k, 'amt': r.get(k)} for k in r]
# [{'so': 'SO1', 'amt': 350}, {'so': 'SO2', 'amt': 200}]
My solution:
d = dict()
for so, amt in map(lambda x: (x["so"],x["amt"]), result):
d[so] = d.get(so, 0) + amt
d is the final dictionary you want. If you have a huge list result, you should take imap instead of map.
Explanation:
map(lambda x: (x["so"],x["amt"]), result) results in the following list:
result = [
('SO1', 250),
('SO2', 200),
('SO1', 100)
]
d.get(so,0) is like d[so] but returns 0 as a default value when d has no key with value so.
Try this..
import collections
data = collections.OrderedDict()
for d in dictlist:
so = d['so']
data.setdefault(so, 0)
data[so] += d['amt']
result = [{'so':k, 'amt': v} for k, v in data.items()]
Hope this helps

Categories