I created a Python script with Python2.7 and it works fine. However, when I run the same script with Python2.6, I got a “SyntaxError: invalid syntax” error.
After investigating, the problem seems to be related to a for loop.
l1 = [["a1", "a2"], ["b1", "b2"]]
print {item[0]:item[1] for item in l1}
When I run the above code with Python 2.7, I’ve got the following output:
{'a1': 'a2', 'b1': 'b2'}
When I run the same code with Python 2.6, I’ve got the following error:
>>> l1 = [["a1", "a2"], ["b1", "b2"]]
>>> print {item[0]:item[1] for item in l1}
File "<stdin>", line 1
print {item[0]:item[1] for item in l1}
^
SyntaxError: invalid syntax
>>>
Any help is appreciated.
Regards,
Allen
Try this:
Edit about your comment: If you want to explicitly select items, wrap them in a tuple: