-1

can someone help me translate some python 2.7 syntax to python 2.6 please (kinda stuck on 2.6 due to redhat dependencies)

so i have a simple function to construct a tree:

def tree(): return defaultdict(tree)

and of course i would like to display the tree in some way. under python 2.7 i can use:

$ /usr/bin/python2.7
Python 2.7.2 (default, Oct 17 2012, 03:00:49)
[GCC 4.4.6 [TWW]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def dicts(t): return {k: dicts(t[k]) for k in t}
...
>>>

all good... but under 2.6 i get the following error:

$ /usr/bin/python
Python 2.6.6 (r266:84292, Sep  4 2013, 07:46:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def dicts(t): return {k: dicts(t[k]) for k in t}
  File "<stdin>", line 1
    def dicts(t): return {k: dicts(t[k]) for k in t}
                                           ^
SyntaxError: invalid syntax

how can i rewrite the code:

def dicts(t): return {k: dicts(t[k]) for k in t}

so that i can use it under python 2.6?

0

2 Answers 2

6

You have to replace the dict comprehension with the dict() being passed a generator expression. ie.

def dicts(t): return dict((k, dicts(t[k])) for k in t)
Sign up to request clarification or add additional context in comments.

Comments

0
def dicts(t):
    d = {}
    for k in t:
        d[k] = dicts(t[k])
    return d

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.