1

I'm not very familiar with python but I need to fix a script which throws a syntax error in version 2.6. Can anybody help to explain the problem?

import pandas as pd
....
d = pd.read_csv(csv_filename, skiprows=skip).to_dict()
d = {k: d[k].values() for k in d}

This is the error message:

d = {k: d[k].values() for k in d}
                            ^
SyntaxError: invalid syntax
Antje Janosch
  • 1,154
  • 5
  • 19
  • 37

1 Answers1

3

Dictionary comprehensions were a new feature in 2.7, and aren't valid syntax in earlier versions. Instead, pass dict a generator expression of two-tuples:

d = dict((k, d[k].values()) for k in d)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • thanks a lot. the error is gone and the keyword 'dict comprehension' helps me to find bits an pieces of documentation to understand both ways. – Antje Janosch Nov 11 '14 at 08:43