-1

When i run the below code in Python 2.6.6, i get a syntax error at for loop. However, it runs fine with no issues in Python 2.7.5

d.update({i: 'value' for i in d if i[-2:] in ('02', '05')})

What modification is needed for it to run in both the versions ?

cool77
  • 1,086
  • 4
  • 15
  • 31
  • 1
    I'm telling you, your `for` loop is perfectly fine :) – miradulo May 09 '16 at 12:51
  • 1
    Yes, you get a syntax error because that syntax doesn't exist in Python 2.6. See the duplicate for an alternative. `dict.update()` takes the same key-value iterable that `dict()` takes, so `d.update((i, 'value') for i in d if i[-2:] in ('02', '05'))` would work here. In fact, that's *preferable* because that syntax doesn't create a new dictionary just to update another. – Martijn Pieters May 09 '16 at 12:53

1 Answers1

1

Dict comprehensions were added in Python2.7 and 3.0. They do not exist in Python2.6.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677