Umut Tabak
Umut Tabak

Reputation: 1942

unpacking a tuple with print

the second unpacking is not working with print, what is the reason?

for a in stok.iteritems():
...  c, b = a
...  print c, b

this one is valid

but this one is not

for a in stok.iteritems():
...  print c, b = a

Upvotes: 2

Views: 674

Answers (4)

Gareth McCaughan
Gareth McCaughan

Reputation: 19981

The reason is that c, b = a is a statement and not an expression (i.e., it does something, but doesn't have a value) and therefore you can't print it.

Upvotes: 2

user2665694
user2665694

Reputation:

Does not make much sense. You want

for a in stok.iteritems():
...  print a

You can not mix assignments within a print...why would you think that this should work? Inventing new syntax?

Upvotes: 1

escargot agile
escargot agile

Reputation: 22389

= is assignment. I'm not sure what you are trying to achieve in the second piece of code, but it doesn't make sense: are you trying to print or are you trying to assign? You cannot print and assign in the same statement.

If you want to compare two numbers, use ==. For example:

print a == b

will tell you whether a and b are equal or not.

Upvotes: 0

Thomas K
Thomas K

Reputation: 40340

You can't do an assignment (a = b) inside a print statement. They're both statements, so they have to be done separately.

If it helps, you can do: for c, b in stok.iteritems():.

Upvotes: 6

Related Questions