Assigning multiple variables to a list a la php's list()

W

Wells

Can you take a list and have it exploded into variables w/ one
command? Something like..


list = ['foo', 'bar']
[a, b] = list

Then 'a' would be foo and 'b' 'bar'.

Like list($a,$b) = $list in PHP.

Thanks!
 
C

Chris Rebert

Can you take a list and have it exploded into variables w/ one
command? Something like..


list = ['foo', 'bar']
[a, b] = list

Then 'a' would be foo and 'b' 'bar'.

Did you think to try it at the interpreter? That exact syntax works,
though we usually write it without the brackets, like:

a, b = list

Also, don't use "list" as a variable name since it clashes with the
builtin type.

Cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top