Can I use list comprehension syntax to create a dictionary?
For example, by iterating over pairs of keys and values:
d = {... for k, v in zip(keys, values)}
Use a dict comprehension (Python 2.7 and later):
{key: value for (key, value) in iterable}
Alternatively for simpler cases or earlier version of Python, use the dict constructor, e.g.:
pairs = [('a', 1), ('b', 2)]
dict(pairs) #=> {'a': 1, 'b': 2}
dict([(k, v+1) for k, v in pairs]) #=> {'a': 2, 'b': 3}
Given separate arrays of keys and values, use the dict constructor with zip:
keys = ['a', 'b']
values = [1, 2]
dict(zip(keys, values)) #=> {'a': 1, 'b': 2}
2) "zip'ped" from two separate iterables of keys/vals
dict(zip(list_of_keys, list_of_values))
In Python 3 and Python 2.7+, dictionary comprehensions look like the below:
d = {k:v for k, v in iterable}
For Python 2.6 or earlier, see fortran's answer.
In fact, you don't even need to iterate over the iterable if it already comprehends some kind of mapping, the dict constructor doing it graciously for you:
>>> ts = [(1, 2), (3, 4), (5, 6)]
>>> dict(ts)
{1: 2, 3: 4, 5: 6}
>>> gen = ((i, i+1) for i in range(1, 6, 2))
>>> gen
<generator object <genexpr> at 0xb7201c5c>
>>> dict(gen)
{1: 2, 3: 4, 5: 6}
Create a dictionary with list comprehension in Python
I like the Python list comprehension syntax.
Can it be used to create dictionaries too? For example, by iterating
over pairs of keys and values:
mydict = {(k,v) for (k,v) in blah blah blah}
You're looking for the phrase "dict comprehension" - it's actually:
mydict = {k: v for k, v in iterable}
Assuming blah blah blah is an iterable of two-tuples - you're so close. Let's create some "blahs" like that:
blahs = [('blah0', 'blah'), ('blah1', 'blah'), ('blah2', 'blah'), ('blah3', 'blah')]
Dict comprehension syntax:
Now the syntax here is the mapping part. What makes this a dict comprehension instead of a set comprehension (which is what your pseudo-code approximates) is the colon, : like below:
mydict = {k: v for k, v in blahs}
And we see that it worked, and should retain insertion order as-of Python 3.7:
>>> mydict
{'blah0': 'blah', 'blah1': 'blah', 'blah2': 'blah', 'blah3': 'blah'}
In Python 2 and up to 3.6, order was not guaranteed:
>>> mydict
{'blah0': 'blah', 'blah1': 'blah', 'blah3': 'blah', 'blah2': 'blah'}
Adding a Filter:
All comprehensions feature a mapping component and a filtering component that you can provide with arbitrary expressions.
So you can add a filter part to the end:
>>> mydict = {k: v for k, v in blahs if not int(k[-1]) % 2}
>>> mydict
{'blah0': 'blah', 'blah2': 'blah'}
Here we are just testing for if the last character is divisible by 2 to filter out data before mapping the keys and values.
In Python 2.7, it goes like:
>>> list1, list2 = ['a', 'b', 'c'], [1,2,3]
>>> dict( zip( list1, list2))
{'a': 1, 'c': 3, 'b': 2}
Zip them!
Python version >= 2.7, do the below:
d = {i: True for i in [1,2,3]}
Python version < 2.7(RIP, 3 July 2010 - 31 December 2019), do the below:
d = dict((i,True) for i in [1,2,3])
To add onto #fortran's answer, if you want to iterate over a list of keys key_list as well as a list of values value_list:
d = dict((key, value) for (key, value) in zip(key_list, value_list))
or
d = {(key, value) for (key, value) in zip(key_list, value_list)}
Just to throw in another example. Imagine you have the following list:
nums = [4,2,2,1,3]
and you want to turn it into a dict where the key is the index and value is the element in the list. You can do so with the following line of code:
{index:nums[index] for index in range(0,len(nums))}
Here is another example of dictionary creation using dict comprehension:
What i am tring to do here is to create a alphabet dictionary where each pair; is the english letter and its corresponding position in english alphabet
>>> import string
>>> dict1 = {value: (int(key) + 1) for key, value in
enumerate(list(string.ascii_lowercase))}
>>> dict1
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8,
'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's':
19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
>>>
Notice the use of enumerate here to get a list of alphabets and their indexes in the list and swapping the alphabets and indices to generate the key value pair for dictionary
Hope it gives a good idea of dictionary comp to you and encourages you to use it more often to make your code compact
This code will create dictionary using list comprehension for multiple lists with different values that can be used for pd.DataFrame()
#Multiple lists
model=['A', 'B', 'C', 'D']
launched=[1983,1984,1984,1984]
discontinued=[1986, 1985, 1984, 1986]
#Dictionary with list comprehension
keys=['model','launched','discontinued']
vals=[model, launched,discontinued]
data = {key:vals[n] for n, key in enumerate(keys)}
#Convert dict to dataframe
df=pd.DataFrame(data)
display(df)
enumerate will pass n to vals to match each key with its list
Try this,
def get_dic_from_two_lists(keys, values):
return { keys[i] : values[i] for i in range(len(keys)) }
Assume we have two lists country and capital
country = ['India', 'Pakistan', 'China']
capital = ['New Delhi', 'Islamabad', 'Beijing']
Then create dictionary from the two lists:
print get_dic_from_two_lists(country, capital)
The output is like this,
{'Pakistan': 'Islamabad', 'China': 'Beijing', 'India': 'New Delhi'}
Adding to #Ekhtiar answer, if you want to make look up dict from list, you can use this:
names = ['a', 'b', 'd', 'f', 'c']
names_to_id = {v:k for k, v in enumerate(names)}
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'f': 4}
Or in rare case that you want to filter duplicate, use set first (best in list of number):
names = ['a', 'b', 'd', 'f', 'd', 'c']
sorted_list = list(set(names))
sorted_list.sort()
names_to_id = {v:k for k, v in enumerate(sorted_list)}
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'f': 4}
names = [1,2,5,5,6,2,1]
names_to_id = {v:k for k, v in enumerate(set(names))}
# {1: 0, 2: 1, 5: 2, 6: 3}
>>> {k: v**3 for (k, v) in zip(string.ascii_lowercase, range(26))}
Python supports dict comprehensions, which allow you to express the creation of dictionaries at runtime using a similarly concise syntax.
A dictionary comprehension takes the form {key: value for (key, value) in iterable}. This syntax was introduced in Python 3 and backported as far as Python 2.7, so you should be able to use it regardless of which version of Python you have installed.
A canonical example is taking two lists and creating a dictionary where the item at each position in the first list becomes a key and the item at the corresponding position in the second list becomes the value.
The zip function used inside this comprehension returns an iterator of tuples, where each element in the tuple is taken from the same position in each of the input iterables. In the example above, the returned iterator contains the tuples (“a”, 1), (“b”, 2), etc.
Output:
{'i': 512, 'e': 64, 'o': 2744, 'h': 343, 'l': 1331, 's': 5832, 'b': 1, 'w': 10648, 'c': 8, 'x': 12167, 'y': 13824, 't': 6859, 'p': 3375, 'd': 27, 'j': 729, 'a': 0, 'z': 15625, 'f': 125, 'q': 4096, 'u': 8000, 'n': 2197, 'm': 1728, 'r': 4913, 'k': 1000, 'g': 216, 'v': 9261}
Yes, it's possible. In python, Comprehension can be used in List, Set, Dictionary, etc.
You can write it this way
mydict = {k:v for (k,v) in blah}
Another detailed example of Dictionary Comprehension with the Conditional Statement and Loop:
parents = [father, mother]
parents = {parent:1 - P["mutation"] if parent in two_genes else 0.5 if parent in one_gene else P["mutation"] for parent in parents}
You can create a new dict for each pair and merge it with the previous dict:
reduce(lambda p, q: {**p, **{q[0]: q[1]}}, bla bla bla, {})
Obviously this approaches requires reduce from functools.
Assuming blah blah blah is a two-tuples list:
Let's see two methods:
# method 1
>>> lst = [('a', 2), ('b', 4), ('c', 6)]
>>> dict(lst)
{'a': 2, 'b': 4, 'c': 6}
# method 2
>>> lst = [('a', 2), ('b', 4), ('c', 6)]
>>> d = {k:v for k, v in lst}
>>> d
{'a': 2, 'b': 4, 'c': 6}
this approach uses iteration over the given date using a for loop.
Syntax: {key: value for (key, value) in data}
Eg:
# create a list comprehension with country and code:
Country_code = [('China', 86), ('USA', 1),
('Ghana', 233), ('Uk', 44)]
# use iterable method to show results
{key: value for (key, value) in Country_code}
Related
What would be the pythonic way to remove elements that are not uniques for certain keys?
Let's say one has a list of dicts such as:
[
{'a': 1, 'b': 'j'},
{'a': 2, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
The expected output would remove the second element, because the key b equals to j in more than one element. Thus:
[
{'a': 1, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
This is what I have tried:
input = [
{'a': 1, 'b': 'j'},
{'a': 2, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
output = []
for input_element in input:
if not output:
output.append(input_element)
else:
for output_element in output:
if input_element['b'] != output_element['b']:
output.append(input_element)
Would the solution be simpler if that'd be a list of tuples, such as:
[(1, 'j'), (2, 'j'), (3, 'i')]
# to produce
[(1, 'j'), (3, 'i')]
Here is an approach using any() and list-comprehension:
Code:
l=[
{'a': 1, 'b': 'j'},
{'a': 2, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
new_l = []
for d in l:
if any([d['b'] == x['b'] for x in new_l]):
continue
new_l.append(d)
print(new_l)
Output:
[{'a': 1, 'b': 'j'}, {'a': 3, 'b': 'i'}]
You could define a custom container class which implements the __eq__ and __hash__ magic methods. That way, you can use a set to remove "duplicates" (according to your criteria). This doesn't necessarily preserve order.
from itertools import starmap
from typing import NamedTuple
class MyTuple(NamedTuple):
a: int
b: str
def __eq__(self, other):
return self.b == other.b
def __hash__(self):
return ord(self.b)
print(set(starmap(MyTuple, [(1, 'j'), (2, 'j'), (3, 'i')])))
Output:
{MyTuple(a=3, b='i'), MyTuple(a=1, b='j')}
>>>
I suggest this implementation:
_missing = object()
def dedupe(iterable, selector=_missing):
"De-duplicate a sequence based on a selector"
keys = set()
if selector is _missing: selector = lambda e: e
for e in iterable:
if selector(e) in keys: continue
keys.add(selector(e))
yield e
Advantages:
Returns a generator:
It iterates the original collection just once, lazily. That could be useful
and/or performatic in some scenarios, specially if you will chain
additional query operations.
input = [{'a': 1, 'b': 'j'}, {'a': 2, 'b': 'j'}, {'a': 3, 'b': 'i'}]
s = dedupe(input, lambda x: x['b'])
s = map(lambda e: e['a'], s)
sum(s) # Only now the list is iterated. Result: 4
Accepts any kind of iterable:
Be it a list, set, dictionary or a custom iterable class. You can construct whatever collection type out of it, without iterating multiple times.
d = {'a': 1, 'b': 1, 'c': 2}
{k: v for k, v in dedupe(d.items(), lambda e: e[1])}
# Result (dict): {'a': 1, 'c': 2}
{*dedupe(d.items(), lambda e: e[1])}
# Result (set of tuples): {('a', 1), ('c', 2)}
Takes an optional selector function (or any callable):
This gives you flexibility to re-use this function in many different contexts, with any custom logic or types. If the selector is absent, it compares the whole elements.
# de-duping based on absolute value:
(*dedupe([-3, -2, -2, -1, 0, 1, 1, 2, 3, 3], abs),)
# Result: (-3, -2, -1, 0)
# de-duping without selector:
(*dedupe([-3, -2, -2, -1, 0, 1, 1, 2, 3, 3]),)
# Result: (-3, -2, -1, 0, 1, 2, 3)
def drop_dup_key(src, key):
''' src is the source list, and key is a function to obtain the key'''
keyset, result = set(), []
for elem in src:
keyval = key(elem)
if keyval not in keyset:
result.append(elem)
keyset.add(keyval)
return result
Use it like this:
drop_dup_key(in_list, lambda d: return d.get('b'))
The comparison of tuples to dictionaries isn't quite accurate since the tuples only contain the dictionary values, not the keys, and I believe you are asking about duplicate key:value pairs.
Here is a solution which I believe solves your problem, but might not be as pythonic as possible.
seen = set()
kept = []
for d in x:
keep = True
for k, v in d.items():
if (k, v) in seen:
keep = False
break
seen.add((k, v))
if keep:
kept.append(d)
print(kept)
Output:
[{'a': 1, 'b': 'j'}, {'a': 3, 'b': 'i'}]
I have a dictionary filled with number strings and i want to convert every of those values into binary. Here is what i tried:
for k,v in ValueDict.items():
ValueDict.update(k:bin(v))
However this does not work. PyCharm says "Illegal target for variable annotation", i dont understand what this means. As far as i know, variable annotation is a way of "commenting" on the types of a variable, but i dont understand how this is related to my issue...
Thanks in advance!
Try using dictionary comprehension:
print({k:bin(v) for k,v in ValueDict.items()})
Or if version less then 2.5:
print(dict((k:bin(v) for k,v in ValueDict.items())))
Your code doesn't work from : in ValueDict.update(k:bin(v))
To use your style:
for k,v in ValueDict.items():
ValueDict.update({k:bin(v)})
Or:
for k,v in ValueDict.items():
ValueDict[k]=bin(v)
You need to provide a dict to update method.
for k,v in ValueDict.items():
ValueDict.update({k:bin(v)})
See the documentation at dict.update:
Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.
update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).
Examples:
# dict expression
d = {"a":1, "b":2, "c":3}
>>> {'a': 1, 'c': 3, 'b': 2}
# multiple updates: no loop needed
d.update( {"a" : 11, "b" : 22} )
>>> {'a': 11, 'c': 3, 'b': 22}
# multiple adds: no loop needed (also no '' around keys)
d.update( A = 111, B = 22 )
>>> {'a': 11, 'A': 111, 'c': 3, 'b': 22, 'B': 22}
# updating all keys using loop
for k,v in d.items():
d[k]=v*2
>>> {'a': 22, 'A': 222, 'c': 6, 'b': 44, 'B': 44}
# add values to a list if same keys and not yet in
# providing an existing key here would overwrite that one
new_ones = ( ("X",42), ("X",4711) )
for k,v in new_ones:
entry = d.setdefault(k,[])
entry.append(v)
>>> {'a': 22, 'A': 222, 'c': 6, 'b': 44, 'B': 44, 'X': [42, 4711]}
# no loop - update from iterable of key/value pairs
new_ones = ( ("i",42), ("j",4711) )
d.update(new_ones)
>>> {'a': 22, 'A': 222, 'c': 6, 'b': 44, 'i': 42, 'j': 4711, 'B': 44, 'X': [42, 4711]}
I have a dictionary composed of {key: value}.
I select a set of keys from this dictionary.
I'd like to build a new dictionary with {keyA: set of all keys wich have the same value as keyA}.
I already have a solution: Is there a faster way to do it?
It seems very slow to me, and I imagine I'm not the only one in this case!
for key1 in selectedkeys:
if key1 not in seen:
seen.add(key1)
equal[key1] = set([key1])#egual to itself
for key2 in selectedkeys:
if key2 not in seen and dico[key1] == dico[key2]:
equal[key1].add(key2)
seen.update(equal[key1])
Try this
>>> a = {1:1, 2:1, 3:2, 4:2}
>>> ret_val = {}
>>> for k, v in a.iteritems():
... ret_val.setdefault(v, []).append(k)
...
>>> ret_val
{1: [1, 2], 2: [3, 4]}
def convert(d):
result = {}
for k, v in d.items(): # or d.iteritems() if using python 2
if v not in result:
result[v] = set()
result[v].add(k)
return result
or just use collections.defaultdict(set) if you are careful enough not to access any non key later :-)
So you want to create a dictionary that maps key to "the set of all keys which have the same value as key" for each selected key in a given source dictionary.
Thus, if the source dictionary is:
{'a': 1, 'b': 2, 'c': 1, 'd': 2, 'e': 3, 'f': 1, 'g': 3)
and the selected keys are a, b, and e, the result should be:
{'a': {'a', 'c', 'f'}, 'e': {'g', 'e'}, 'b': {'b', 'd'}}
One way to achieve this would be to use a defaultdict to build a value to key table, and then use that to build the required result from the specified keys:
from collections import defaultdict
def value_map(source, keys):
table = defaultdict(set)
for key, value in source.items():
table[value].add(key)
return {key: table[source[key]] for key in keys}
source = {'a': 1, 'b': 2, 'c': 1, 'd': 2, 'e': 3, 'f': 1, 'g': 3)
print(value_map(source, ['a', 'b', 'e']))
Output:
{'a': {'a', 'c', 'f'}, 'e': {'g', 'e'}, 'b': {'b', 'd'}}
Since you select a set of keys from the original dictionary. We can modify #Nilesh solution for your purpose.
a = {1:1, 2:1, 3:2, 4:2}
keys = [1, 3] # lets say this is the list of keys
ret_val = {}
for i in keys:
for k,v in a.items():
if a[i]==v:
ret_val.setdefault(i, []).append(k)
print (ret_val)
{1: [1, 2], 3: [3, 4]}
This was sort of stated in the comments by #Patrick Haugh:
d=your dictionary
s=set(d.values())
d2={i:[] for i in s}
for k in d:
d2[d[k]].append(k)
I have a list of dictionaries and I want to select the items corresponding to the keys that are in a a separate list (note: we can assume that each dict contains the keys). Furthermore I want to process a subset of the selected items.
As an example, given:
keys_of_interest = ['a', 'b']
all_items =[
{'a' : value_a1, 'b' : value_b1, 'c' : ...},
{'a' : value_a2, 'b' : value_b2, 'c' : ...},
...
]
The result I want is obtained by extracting the values corresponding to a and b and apply fun only to a, i.e.:
[
[fun(value_a1), value_b1],
[fun(value_a2), value_b2],
...
]
The key-extraction part can be accomplished via:
from operator import itemgetter
[itemgetter(*keys_of_interest)(el) for el in all_items]
Is there any (elegant) way to combine itemgetter with a function
If elegance is the priority, you can transform the dicts first, and then use itemgetter without further tweaking. To do this you need to separate the keys for which you want to apply a function from those for which you just want the original values -- which I think is a good thing to do in any case.
First, set up
keys_of_interest = ['a', 'b', 'c', 'd']
keys_for_func = ['a', 'b'] # with this approach you can apply a func to multiple keys
func = lambda x: x ** 2 # for demo
all_dicts = [{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5},
{'a': 6, 'b': 7, 'c': 8, 'd': 9, 'e': 10}]
Then this is what I'd do:
transformed = ({k: func(v) if k in keys_for_func else v for k, v in d.items()}
for d in all_dicts)
result = list(map(itemgetter(*keys_of_interest), transformed))
Result:
[(1, 4, 3, 4), (36, 49, 8, 9)]
There probably isn't a super tidy way to do this. One way might be:
getter = itemgetter(*keys_of_interest[1:])
def process_dict(d):
return_value = [fun(d[keys_of_interest[0]])]
return_value.extend(getter(d))
return return_value
result = [process_dict(d) for d in all_items]
If you're targeting a recent-enough version of python, this can be done similarly with a bit less subscripting...
first_key, *other_keys = keys_of_interest
getter = itemgetter(*other_keys)
def process_dict(d):
return_value = [fun(d[first_key])]
return_value.extend(getter(d))
return return_value
Is this too inelegant?
from operator import itemgetter
result = [(f(x), *others) for x, *others in [
itemgetter(*keys_of_interest)(el) for el in all_items]
]
Or in older versions:
from operator import itemgetter
result = [(f(x[0]), x[1:]) for x in [
itemgetter(*keys_of_interest)(el) for el in all_items]
]
Python 3.5+ solution:
setup:
>>> from operator import itemgetter
>>> keys_of_interest = ['a', 'b', 'c'] # 3 keys to show unpacking at work
>>> all_items = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 5, 'b': 6, 'c': 7, 'd': 8}]
>>> fun = str # some silly demo function
Apply map and the Python 3.5 unpacking:
>>> list(map(lambda x: (fun(x[0]), *x[1:]),
... (itemgetter(*keys_of_interest)(el) for el in all_items)))
[('1', 2, 3), ('5', 6, 7)]
I want to write a code which takes the following inputs:
list (list of maps)
request_keys (list of strings)
operation (add,substract,multiply,concat)
The code would look at the list for the maps having the same value for all keys except the keys given in request_keys. Upon finding two maps for which the value in the search keys match, the code would do the operation (add,multiple,substract,concat) on the two maps and combine them into one map. This combination map would basically replace the other two maps.
i have written the following peice of code to do this. The code only does add operation. It can be extended to make the other operations
In [83]: list
Out[83]:
[{'a': 2, 'b': 3, 'c': 10},
{'a': 2, 'b': 3, 'c': 3},
{'a': 2, 'b': 4, 'c': 4},
{'a': 2, 'b': 3, 'c': 2},
{'a': 2, 'b': 3, 'c': 3}]
In [84]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def func(list,request_keys):
: new_list = []
: found_indexes = []
: for i in range(0,len(list)):
: new_item = list[i]
: if i in found_indexes:
: continue
: for j in range(0,len(list)):
: if i != j and {k: v for k,v in list[i].iteritems() if k not in request_keys} == {k: v for k,v in list[j].iteritems() if k not in request_keys}:
: found_indexes.append(j)
: for request_key in request_keys:
: new_item[request_key] += list[j][request_key]
: new_list.append(new_item)
: return new_list
:--
In [85]: func(list,['c'])
Out[85]: [{'a': 2, 'b': 3, 'c': 18}, {'a': 2, 'b': 4, 'c': 4}]
In [86]:
What i want to know is, is there a faster, more memory efficient, cleaner and a more pythonic way of doing the same?
Thank you
You manually generate all the combinations and then compare each of those combinations. This is pretty wasteful. Instead, I suggest grouping the dictionaries in another dictionary by their matching keys, then adding the "same" dictionaries. Also, you forgot the operator parameter.
import collections, operator, functools
def func(lst, request_keys, op=operator.add):
matching_dicts = collections.defaultdict(list)
for d in lst:
key = tuple(sorted(((k, d[k]) for k in d if k not in request_keys)))
matching_dicts[key].append(d)
for group in matching_dicts.values():
merged = dict(group[0])
merged.update({key: functools.reduce(op, (g[key] for g in group))
for key in request_keys})
yield merged
What this does: First, it creates a dictionary, mapping the key-value pairs that have to be equal for two dictionaries to match to all those dictionaries that have those key-value pairs. Then it iterates the dicts from those groups, using one of that group as a prototype and updating it with the sum (or product, or whatever, depending on the operator) of the all the dicts in that group for the required_keys.
Note that this returns a generator. If you want a list, just call it like list(func(...)), or accumulate the merged dicts in a list and return that list.
from itertools import groupby
from operator import itemgetter
def mergeDic(inputData, request_keys):
keys = inputData[0].keys()
comparedKeys = [item for item in keys if item not in request_keys]
grouper = itemgetter(*comparedKeys)
result = []
for key, grp in groupby(sorted(inputData, key = grouper), grouper):
temp_dict = dict(zip(comparedKeys, key))
for request_key in request_keys:
temp_dict[request_key] = sum(item[request_key] for item in grp)
result.append(temp_dict)
return result
inputData = [{'a': 2, 'b': 3, 'c': 10},
{'a': 2, 'b': 3, 'c': 3},
{'a': 2, 'b': 4, 'c': 4},
{'a': 2, 'b': 3, 'c': 2},
{'a': 2, 'b': 3, 'c': 3}]
from pprint import pprint
pprint(mergeDic(inputData,['c']))