Merge two dictionaries in Python (2.7)

Disclaimer:
This matter is extent. There are a number of different ways to do the same thing. My goal is to have an easy-to-do shortcut at hand to use it when I need it. See a link below for the source article with more information and discussion on the topic.

Dictionaries to be merged into a third one, z:

>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}

To merge them:

>>> z = x.copy()
>>> z.update(y)

which returns:

>>> z
{'a': 1, 'b': 3, 'c': 4}

Note that the value of 'b' was the last one updated, coming from the y dictionary.

For further details on this topic, please visit the source article at Stack Overflow.

Thanks!

IPython (& ipdb) issue with python 2.7

If you ever wanted to use ipdb (from IPython) debugger when using Python 2.7, you might have faced an issue like this:

$ sudo pip install ipython
Downloading/unpacking ipython
  Downloading ipython-6.0.0.tar.gz (5.1MB): 5.1MB downloaded
  Running setup.py (path:/tmp/pip_build_root/ipython/setup.py) egg_info for package ipython
    
    IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.
    When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
    Beginning with IPython 6.0, Python 3.3 and above is required.
...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/ipython

So you gotta go ahead and install IPython==5.0, because it’s the last LTS version. Do:

#pip install IPython==5.0

And then you should get:

ImportError: No module named packaging.version

Hm, that was not expected. Installing setuptools or packaging did not work for me at this moment. To get away with this error I had to install pip3 and then upgrade pip:

  251  sudo apt-get install python3-pip
  252  sudo pip3 install --upgrade pip
  253  sudo pip install ipython==5.0
  254  sudo pip install ipdb

After that, both IPython and ipdb installed with no issues or errors and life was happy again.

ALTERNATIVE SOLUTION:

If the above still does not fix the issue, or if pip and ipython are bound to python 3, you can try this, according described in the packaging.python.org website:

  1. Download get-pip.py
  2. Run python get-pip.py. [2] This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.

Let me know if that worked for you too.

Good Luck!