2

I have a line of code that creates a dictionary by setting its key as that of the key of dictionary_b. It then uses the value of that key in dictionary_b as the key to look through dictionary_a to grab the corresponding value.

new_dictionary = {key : dictionary_a.get(value, value) for key, value in dictionary_b.iteritems()}

This compiles fine on Windows however when I try to use compile on Linux I get the following error:

  File "IP_Merger.py", line 46
    new_ip = {key : dictionary_a.get(value, value) for key, value in dictionary_b.iteritems()}
                                                     ^
SyntaxError: invalid syntax

On Windows I am compiling on Python 2.7 and on Linux 2.6.6. Could that be the issue?

algorhythm
  • 3,304
  • 6
  • 36
  • 56

1 Answers1

3

Dictionary comprehensions are supported starting in Python 2.7 - see this quesiton for more information.

You can create a dictionary with a generator as follows in python 2.6:

dict((key(s),value(s)) for s in something)
Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65