user1919
user1919

Reputation: 3938

Program works fine in PyDev but gives invalid syntax in Ubuntu

This question must be quite easy but as I am new in both Ubuntu and Python, I have problems finding the solution.

I have written a program in Python using PyDev on Windows. Everything works fine and I get the output I want. Now I am trying to run this program on Ubuntu Linux. I have installed all the necessary modules and I run from the terminal the command:

python home/project/bin/prog/main.py

It starts executing but then I get an error:

SyntaxError: invalid syntax
Line 128 
dict_values = {z[length_arr]:list(z[:length_arr]) for z in zip(*list_of_lists)} 

So I guess there is a difference in the syntax between the Python version I use in Windows and the one in the Ubuntu (Python 2.6.5).

I tried to write the code like this:

for z in zip(*list_of_lists):
    dict_values = {z[length_arr]:list(z[:length_arr])} 

But I think is not the same.

How can I write this part of a code so that I don't get invalid syntax? What would be an appropriate syntax?

Upvotes: 0

Views: 121

Answers (1)

DSM
DSM

Reputation: 353039

This line:

dict_values = {z[length_arr]:list(z[:length_arr]) for z in zip(*list_of_lists)} 

is a dict comprehension, which wasn't introduced until Python 2.7. You can rewrite it using dict and a generator expression instead:

dict_values = dict((z[length_arr], list(z[:length_arr])) for z in zip(*list_of_lists))

which will work in both Python 2.6 and 2.7.

Upvotes: 4

Related Questions