Compact way to assign values by slicing list in Python

A

alex23

I prefere this one:

bar = ['a','b','c','x','y','z']
v1, _, _, v2, v3, _ = bar

I also like the solution with itemgetter:

v1, v2, v3 = itemgetter(0, 3, 4)(bar)

but I think it is less readable than the previous one

What if you wanted the 2nd, 37th, and 1007th items from a list?

Personally, I find the 2nd form far more readable, once I got past my
initial surprise that it would access a list of items.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top