mvexel
mvexel

Reputation: 1113

Python function expects a tuple, what I have is a list. How do I call this function?

The function record() in the pyshp module expects a sequence as input:

outfile.record('First','Second','Third')

What I have is a list:

row = ['First','Second','Third']

When I call the record() function like this:

outfile.record(row)

I get a tuple index out of range error. It turns out the function receives

(['First','Second','Third'],)

How do I call record correctly? I have tried

outfile.record((row[i] for i in range(len(row)))

but that doesn't work either.

Upvotes: 2

Views: 1782

Answers (2)

Francis Avila
Francis Avila

Reputation: 31631

outfile.record(*row)

This will unpack a sequence into individual arguments. This is a formal description of this syntax from the language reference, and this is an informal description from the tutorial.

Note there is a similar construct which will unpack a mapping (dict) into keyword arguments:

functiontakingkeywordarguments(**mydict)

Upvotes: 12

chroipahtz
chroipahtz

Reputation: 971

outfile.record(*row)

The * in this case means "unpack." It will unpack a list into a series of arguments.

http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

Upvotes: 7

Related Questions