Reputation: 15192
I want to take a list [0,1,2]
and turn it into [0,1,2,2,1,0]
. Right now, I've got
r = list(mus)
r.reverse()
mus = mus + r
but it seems like there should be a better way. Can anyone come up with a good, pythonic one-liner?
Upvotes: 1
Views: 710
Reputation: 1072
Just a few of the many ways to do this are:
The first two assign the output to a new list, and the second two update the list in place. If I was being clever, I would probably use 2 or 4, but if I wanted there to be no mistake about what I was doing, then I would opt for 1 or 3.
Hope this helps.
Edit: As pointed out in the comments, #1 doesn't work, as reversed returns an iterator. You could make it work by changing it to:
m = l + list(reversed(l))
But I can't recommend it. Go with 2 instead.
BTW, thanks for the correction and sorry for any confusion.
Upvotes: 2
Reputation: 76753
It looks like you might be in need of
mus.extend(reversed(mus))
Or if you simply need to iterate over this and not necessarily form the list, use
import itertools
for item in itertools.chain(mus, reversed(mus)):
do_something...
Upvotes: 3