2

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?

1
  • 1
    Isn't there a 1 too many, at the start? That is to say; you prepended a 1 and put 1s between the original values. Commented Dec 8, 2013 at 3:56

5 Answers 5

4

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))
Sign up to request clarification or add additional context in comments.

4 Comments

@DavidUnric: well, I wouldn't promise that it wins a performance shootout, but it might well be THE first version :-)
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 :)
@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 :-)
@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 ;-)
2
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]

1 Comment

Yes, I discovered this too; see my comment on the question
2

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]

Comments

1

A simple for loop would suffice

for item in list:
  newList.append(1)
  newList.append(item)
list = newList

2 Comments

Just newList.pop() removes the last element. Swap the appends() and lose the .pop() however, to match the OP sample output.
@MartijnPieters Ah, I was reading the sample output opposite it seems.
0

Using itertools:

>>> L=[1,2,3,4,5,6,7,8,9]
>>> from itertools import chain, izip, repeat
>>> list(chain.from_iterable(izip(repeat(1), L)))
[1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.