0

Quick question... searched and didn't find anything. I have this list:

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

I want to have each value repeated 3 more times in the same list... How do I do so? I tried some for loops that .append to the list but things got messy. I ended up getting some list in list that were in lists. I have a feeling .append is not right for this scenario.

1
  • [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,] Basically 4 more of everything Commented Feb 14, 2015 at 1:39

5 Answers 5

4
In [1]: my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

In [2]: sorted(my_list * 3)

Note that you should not use list as a variable name, because it shadows the keyword list.

One other option is to use numpy:

In [8]: import numpy as np

In [9]: np.repeat(my_list, 3)
Sign up to request clarification or add additional context in comments.

Comments

2

Use nested loops in a list comprehension.

>>> [x for x in L for y in range(4)]
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

Comments

1

You can try something like this!

baselist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
[z for y in [[x]*4 for x in baselist] for z in y]

This is equivalent to:

listofLists = []
for x in baseList:
    listofLists.append([x]*4)

finalList = []
for y in listofLists:
    for z in y:
        finalList.append(z)

You see, the list comprehension simply shortens the logic, but whether it's more readable will depend on your grasp of comprehension syntax.

5 Comments

Sweeeet, thanks everyone for the fast replies... I was trying some list comprehensions too but I was doing them all wrong.
@tear728 I recommend you choose Avinash Raj's or Ignacio's answer on this one. Their solutions make a lot more sense from all perspectives (efficiency, readability, etc). Thank you, though!
Eithos I used yours but I did not end up getting the desired answer =[ I got [1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] notice four 1's , 20 10's and 5 of everything else! Thanks anyways though!
@tear728 You should double-check that you've entered it correctly. I've just run through it again to be sure, and it gives exactly the right output (4x everything). The only explanation I can think of is you used *5 instead of *4 and somehow removed one of the 1s. I don't mind that you went with Avinash's answer, but this one works exactly like you asked, too.
you are totally right, I just found the bug, what you had works too. Thanks again for the hep!
1

You could do like this,

>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
>>> [j for i in lst for j in [i,i,i,i]]
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

3 Comments

Great solution! This is how you should write the list comp if that's the way you choose to go @tear728. +1
Actually Ignacio's answer is better in that (a) it doesn't require repeatedly creating and destroying sub-lists and (b) it makes it really easy to vary how many times each item gets copied - it stores it explicitly as a number instead of implicitly as sub-list structure.
@HughBothwell Agreed. I didn't notice it until now... :0
0

Another itertools variation using the flatten recipe

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

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.