Jetlef
Jetlef

Reputation: 139

Using .split() inside for loops in Python gives an error

Using the following code:

def printformatted(statuses):
    for status in statuses:
        statusid, statussummary = status.split(",",1)
        print "\nSnapshot id: %s" % statusid
        print   "Summary:     %s" % statussummary
    print

printformatted("1,Some summary") gives me the error ValueError: need more than 1 value to unpack, whereas printformatted(["1,Some summary"]) does not. Why?

Upvotes: 0

Views: 195

Answers (1)

shx2
shx2

Reputation: 64298

In the first case, you're passing a string, so for status in statuses iterates over the string, character by character, which is not what you want.

In the second case, you're passing a list, so for status in statuses iterates over its elements, first element being "1,Some summary".

Upvotes: 2

Related Questions