To merge two dictionaries of list in Python

athira

There are two dictionaries

x={1:['a','b','c']}
y={1:['d','e','f'],2:['g']}

I want another dictionary z which is a merged one of x and y such that

z = {1:['a','b','c','d','e','f'],2:['g']}

Is it possible to do this operation? I tried update operation

x.update(y)

But it gives me the following result

z= {1:['d','e','f'],2:['g']}
nitishagar

If you choose Python2.7 in your project. Counter() can be used in this case:

Python 2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x={1:['a','b','c']}
>>> y={1:['d','e','f'],2:['g']}
>>> from collections import Counter
>>> Counter(x) + Counter(y)
Counter({2: ['g'], 1: ['a', 'b', 'c', 'd', 'e', 'f']})

In Python 3.x, what if you run the same code, you will see:

Python 3.9.0 (v3.9.0:9cf6752276, Oct  5 2020, 11:29:23) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: x={1:['a','b','c']}

In [2]: y={1:['d','e','f'],2:['g']}

In [3]: from collections import Counter

In [4]: Counter(x) + Counter(y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-0aa8b00837b3> in <module>
----> 1 Counter(x) + Counter(y)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/collections/__init__.py in __add__(self, other)
    759         for elem, count in self.items():
    760             newcount = count + other[elem]
--> 761             if newcount > 0:
    762                 result[elem] = newcount
    763         for elem, count in other.items():

TypeError: '>' not supported between instances of 'list' and 'int'

Let's take a look at the source code at vim /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/collections/__init__.py +761

To check the different between Python2.x and Python3.x, we will find the path of collections in python2. to do that just simply execute a wrong call like:

>>> Counter(9,1) # wrong way to call.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 475, in __init__
    raise TypeError('expected at most 1 arguments, got %d' % len(args))
TypeError: expected at most 1 arguments, got 2

and then, you see /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py.

Okay, now open both files and locate to the __add__ function in class Counter.

vim -d /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/collections/__init__.py +761 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py

But we could find the deferent in this snipe code.

enter image description here

Think about this, you can see the TypeError: '>' not supported between instances of 'list' and 'int' show two important things:

  • > reflect to def __gt__(self, value, /) method in class dict. so let's check the different between python2 and python3 dict.
  • 'list' and 'int'. to make sure we meet the same type here, just add a line to print it out. before do compare line if newcount > 0: like this way:
print("newcount is --> ", newcount, type(newcount))
if newcount > 0:

and you will see this:

enter image description here

Not surprise, right? we could compare list to int, em makes sense..

let's quickly compare a list with int in python 2.

enter image description here It's returning a True. keep dived into the Python3.x

enter image description here

Okay, now you should be clear about what's going on. to fix this, should be easily do some change on your own python env or commit it to git. enter image description here

Finally, you got two good news, you fix the Python3.x bug. and you got your result.

enter image description here

If desired result is a dict. You can use the following:

z = dict(Counter(x) + Counter(y))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Merge two list into a list of dictionaries with multiple keys in python

From Dev

Python - merge two list of dictionaries adding values of repeated keys

From Dev

Merge two dictionaries in python with lists as values

From Dev

Merge two 2D dictionaries python

From Dev

Merge two dictionaries in python with lists as values

From Dev

python merge list of dictionaries based on key

From Dev

merge two dictionaries by key

From Dev

merge two dictionaries by key

From Dev

Merge two dictionaries

From Dev

How to use Lambda to merge two Dictionary with List<string> contain in Dictionaries

From Dev

In Python merge two dictionaries so that their keys are added/subtracted

From Dev

Merge two dictionaries and keep the values for duplicate keys in Python

From Dev

Merge three dictionaries in Python

From Dev

How to merge two list of list in python

From Dev

Generic function to merge two dictionaries

From Dev

How to merge two dictionaries in javascript

From Java

How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

From Dev

Merge two dictionaries with nested dictionaries into new one, summing same keys and keeping unaltered ones in Python

From Dev

Compare two List of Dictionaries

From Dev

Efficiently merge lists into a list of dictionaries

From Dev

Efficiently merge lists into a list of dictionaries

From Dev

Python List to List of Dictionaries

From Dev

How to merge two elements in a list in Python

From Dev

How should two Python datasets that are based on a list of dictionaries be added?

From Dev

Create unique dictionaries for two keys in a list of dictionaries

From Java

Intersecting two dictionaries in Python

From Dev

compare two dictionaries in python

From Dev

merge list of dictionaries which have list init

From Dev

merge list of dictionaries which have list init

Related Related

  1. 1

    Merge two list into a list of dictionaries with multiple keys in python

  2. 2

    Python - merge two list of dictionaries adding values of repeated keys

  3. 3

    Merge two dictionaries in python with lists as values

  4. 4

    Merge two 2D dictionaries python

  5. 5

    Merge two dictionaries in python with lists as values

  6. 6

    python merge list of dictionaries based on key

  7. 7

    merge two dictionaries by key

  8. 8

    merge two dictionaries by key

  9. 9

    Merge two dictionaries

  10. 10

    How to use Lambda to merge two Dictionary with List<string> contain in Dictionaries

  11. 11

    In Python merge two dictionaries so that their keys are added/subtracted

  12. 12

    Merge two dictionaries and keep the values for duplicate keys in Python

  13. 13

    Merge three dictionaries in Python

  14. 14

    How to merge two list of list in python

  15. 15

    Generic function to merge two dictionaries

  16. 16

    How to merge two dictionaries in javascript

  17. 17

    How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

  18. 18

    Merge two dictionaries with nested dictionaries into new one, summing same keys and keeping unaltered ones in Python

  19. 19

    Compare two List of Dictionaries

  20. 20

    Efficiently merge lists into a list of dictionaries

  21. 21

    Efficiently merge lists into a list of dictionaries

  22. 22

    Python List to List of Dictionaries

  23. 23

    How to merge two elements in a list in Python

  24. 24

    How should two Python datasets that are based on a list of dictionaries be added?

  25. 25

    Create unique dictionaries for two keys in a list of dictionaries

  26. 26

    Intersecting two dictionaries in Python

  27. 27

    compare two dictionaries in python

  28. 28

    merge list of dictionaries which have list init

  29. 29

    merge list of dictionaries which have list init

HotTag

Archive