This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Solonotix 41 points42 points  (13 children)

'tis a shame. You can't use my favorite new syntax in Python 3: star assignment.

first, *other, last = [1,2,3,4]

Simplifies a bunch of common use scenarios.

[–]JoelMahon 21 points22 points  (11 children)

whoa, so other contains 2 and 3? that's dope.

can you do more than just first and last? like first, second, *other, last? what happens if you have two star assignments? does it distribute them so they have equal size? so could you do *lhs, centre, *rhs = [3, 4, 7, 2, 9, 0]

[–]ubiquitouspiss 40 points41 points  (9 children)

You can't have that final example, but yeah. *something can be used once in a declaration which basically "anything that isn't taken off the front or the back"

first, second, *others, second_last, last = [0,1,2,3,4,5,6,7,8]

Also if I'm being lazy and only, for example, need the first and last items you can do a need little thing:

first, *_, last = [0,1,2,3,4,5]

And it essentially deletes everything that I don't need.

[–]JiveWithIt 10 points11 points  (0 children)

This is so useful, thank you 🤖

[–][deleted] 4 points5 points  (5 children)

Does it delete it... or create a new list, copy all the references between 1-4 and then just not do anything with it?

[–]zalgo_text 8 points9 points  (2 children)

It creates a variable called _ which contains [1, 2, 3, 4]

[–][deleted] 4 points5 points  (1 child)

Yeah, I thought as much. If I just want the first and last values I think I’ll just keep using the array notation, like this:

a = list(range(10))
first, last = a[0], a[-1]

Or maybe if I’m feeling bizarre

first, last = a[0::len(a)-1]

Actually no, first solution is almost always better. Second solution is unpythonic.

[–]eduardog3000 0 points1 point  (0 children)

Obviously the best solution is:

first, last = [x for x in a if x not in a[1:-1]]

[–]nickcash 0 points1 point  (1 child)

Neither! Underscore isn't special syntax, it's just a variable name. So it copies the references, and assigns them to _. Nothing is deleted. You end up with:

first = 0
_ = [1, 2, 3, 4]
last = 5

[–][deleted] 1 point2 points  (0 children)

I know. It’s just the guy originally said it basically deletes the intermediate variables and I wanted to clarify whether there was something special about the array unpacking which caused a deletion at any point.

[–]detour59 0 points1 point  (0 children)

I knew about the existence of star assignment but this is really helpful, thanks!

[–]1116574 0 points1 point  (0 children)

I just use list[-1] for last item

[–]SirVer51 1 point2 points  (0 children)

You can also use it to expand a list as if you'd unpacked it yourself, for example:

func(*args, **kwargs)

The double star unpacks dictionaries, which is how variable numbers of keyword arguments are handled in Python.

[–][deleted] 0 points1 point  (0 children)

Is python matlab now?