I want to make from the list:
L=[1,2,3,4,5,6,7,8,9]
This:
L=[1,1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9]
This is, put the 1 between the objects in list. Can someone help me?
asked Dec 8, 2013 at 3:51
iam_agf
6892 gold badges10 silver badges20 bronze badges
-
1Isn't there a 1 too many, at the start? That is to say; you prepended a 1 and put 1s between the original values.Martijn Pieters– Martijn Pieters2013年12月08日 03:56:23 +00:00Commented Dec 8, 2013 at 3:56
5 Answers 5
For the result in your example (1 before each object):
L = [y for x in L for y in (1, x)]
For the result described by your text (1 between the objects):
L = [y for x in L for y in (x, 1)]
L.pop()
If you hate multiple for clauses in comprehensions:
L = list(itertools.chain.from_iterable((1, x) for x in L))
answered Dec 8, 2013 at 4:10
Steve Jessop
281k40 gold badges473 silver badges709 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Steve Jessop
@DavidUnric: well, I wouldn't promise that it wins a performance shootout, but it might well be THE first version :-)
mgilson
It's very nice usage of nested list-comps. If I didn't really dislike nested list comprehensions, I would really like this answer. +1 :)
Steve Jessop
@mgilson: would it help at all to write it across several lines, so each
for clause is on its own line? Most comprehensions with multiple for clauses that I use are still way simpler than the average SQL statement with an equal number of joins. They can be easier or harder to read according to how they're presented :-)mgilson
@SteveJessop -- maybe. For some reason, these have always just been mind-benders for me. The itertools solution is nice too. Both ways, it's a great answer. And yeah, definitely simpler than an average SQL statement. A lot simpler than the average regex too ... But I hope our bar for readability is a lot higher than the average readability in those two languages ;-)
print ([i for t in zip([1] * len(L), L) for i in t])
Output
[1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9]
answered Dec 8, 2013 at 3:54
thefourtheye
241k53 gold badges466 silver badges505 bronze badges
1 Comment
Martijn Pieters
Yes, I discovered this too; see my comment on the question
I've always really loved slice assignment:
>>> L = range(1, 10)
>>> L
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> # Done with setup, let the fun commence!
>>> LL = [None] * (len(L)*2) # Make space for the output
>>> LL[1::2] = L
>>> LL[::2] = [1] * len(L)
>>> LL # Lets check the results, shall we?
[1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9]
answered Dec 8, 2013 at 3:59
mgilson
312k70 gold badges658 silver badges723 bronze badges
Comments
A simple for loop would suffice
for item in list:
newList.append(1)
newList.append(item)
list = newList
answered Dec 8, 2013 at 3:54
Dylan Lawrence
1,53311 silver badges35 bronze badges
2 Comments
Martijn Pieters
Just
newList.pop() removes the last element. Swap the appends() and lose the .pop() however, to match the OP sample output.Dylan Lawrence
@MartijnPieters Ah, I was reading the sample output opposite it seems.
lang-py