0

I'm writting some scripts in Python2.6. I always write in python2.7 so I don't know why this sentence is wrong:

keys = ['h','b']
d = {k:0 for k in keys if not k in ['time_us', 'status']}

The error:

print (sys.version)

2.6.6 (r266:84292, Mar 15 2018, 13:11:05) [GCC 5.4.0 20160609]

keys = ['b','h']

d = {k:0 for k in keys if not k in ['time_us', 'status']}

File "", line 1

d = {k:0 for k in keys if not k in ['time_us', 'status']}
           ^ SyntaxError: invalid syntax
Community
  • 1
  • 1
Miguel.G
  • 377
  • 1
  • 6
  • 20

1 Answers1

3

Dictionary comprehensions came along in Python 2.7.

For Python 2.6, you can pass dict a sequence of key-value pairs.

d = dict((k,0) for k in keys if k not in ('time_us', 'status'))
khelwood
  • 55,782
  • 14
  • 81
  • 108