0

I have read alot of strange syntaxerror questions and have not seen mine among it yet and I am really at a loss. I am doing some homework for which the deadline is coming closer and this error I cant get rid of:

def create_voting_dict():
    strlist = [voting_data[i].split() for i in range(len(voting_data))]
    return voting_dict = {strlist[h][0]:[int(strlist[h][g]) for g in range(3, len(strlist[h]))] for h in range(len(strlist))}

Which gets me the error:

return voting_dict = {strlist[h][0]:[int(strlist[h][g]) for g in range(3, len(strlist[h]))] for h in range(len(strlist))}
                                                                                        ^         
SyntaxError: invalid syntax

This error did not occur when I defined voting_dict inside the procedure, but I need to define it globally so i put it after return and then I got the error. Have been counting parenthesis all over but that doesnt seem to be the problem.

I am sure that when I see the problem it is very easy, but I just dont see it. Thanks for any help.

*voting data is a list with strings and I made the procedure to split the strings and create a dictionary

3
  • 1
    Why are you overcomplicating things by using all these range(len(…)) calls? Why not just {outer[0]: [int(inner) for inner in outer[3:]] for outer in strlist]}? Commented Jul 31, 2013 at 18:53
  • 1
    Also, in general, if you get a syntax error on a 121-character line with a mess of parens, brackets, and braces, your first step in debugging should be to break it into smaller pieces and figure out which piece has the error. In fact, even if you didn't have an error here, can you really understand that line written as-is? (Certainly you can't in an 80-column window, like a SO question or a typical terminal… But even ignoring that, it's pretty hard to understand.) Commented Jul 31, 2013 at 18:55
  • It took me some reading, but indeed the formulation in your first comment would make it less complicated. As for your second comment I will take it to heart for my next bug :) Thanks. My programming experience is limited. Commented Jul 31, 2013 at 19:22

3 Answers 3

6

You cannot define in a return. (Because assignments do not return values) Just do

return {strlist[h][0]:[int(strlist[h][g]) for g in range(3, len(strlist[h]))] for h in range(len(strlist))}

Or define a voting_dict in a new statement and then return voting_dict.

See the example -

>>> def test():
        return num = 2
SyntaxError: invalid syntax
>>> def test():
        return 2
Sign up to request clarification or add additional context in comments.

5 Comments

I can and will do so in 10 minutes :) Subsequent procedures use the voting_dict created by this procedure, however when I run them I get the error global voting_dict not defined. Any idea how that is caused? That was the reason I tried to put the definition in the return statement.
If your voting_dict is a global variable, you need a global voting_dict statement in the function.
Else, you can just return the dictionary and access it using the callee function.
@SukritKalra: +1 for the second version of your comment. It's hard to be sure without seeing the code, but it's pretty likely that voting_dict doesn't need to be a global variable at all, and even if it does, it's almost certain that create_voting_dict doesn't need to see or modify it (since it's returning the value).
You are right, the functions worked without having it global but when the script that grades my homework ran over it I got the error that it was not global. So that was probably something with how I defined my functions and how the grading script calls them. Setting it global resolved it. Thank you for your time guys.
1

Its problem with your return statement in which you cannot carry out assignments. Just do it a step before.

Comments

1

If you want to create/populate a global variable voting_dict, then do:

def create_voting_dict():
    strlist= [voting_data[i].split() for i in range(len(voting_data))]
    global voting_dict
    voting_dict= {strlist[h][0]:[int(strlist[h][g]) for g in range(3, len(strlist))}

create_voting_dict()

or

def create_voting_dict():
    strlist= [voting_data[i].split() for i in range(len(voting_data))]
    return {strlist[h][0]:[int(strlist[h][g]) for g in range(3, len(strlist))}

voting_dict= create_voting_dict()

or even

def create_voting_dict(vd):
    strlist= [vd[i].split() for i in range(len(vd))]
    return {strlist[h][0]:[int(strlist[h][g]) for g in range(3, len(strlist))}

voting_dict= create_voting_dict(voting_data)

The advantage of the later is that it's more general and thus can be used in other situations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.