1

I want to duplicate a list n number of times while preserving the order of the list itself. For example all I have right now and it doesn't work nor does it give me an error.

def duplicate(testList, n):
    y = 0
    x = len(testList)
    newList=[]
    while y < x:
        for z in range (0, n):
            newList.append(testList[y])
        y = y + 1
    return newList

duplicate([1,2,3], 3)

After this im not sure where to fix it.

The desired output is:

[1,1,1,2,2,2,3,3,3]
Georgy
  • 12,464
  • 7
  • 65
  • 73
Frank Valdez
  • 77
  • 1
  • 1
  • 9

4 Answers4

10

How about:

>>> def duplicate(testList, n):
...     return [ele for ele in testList for _ in range(n)]
... 
>>> duplicate([1,2,3],2)
[1, 1, 2, 2, 3, 3]
>>> duplicate([1,2,3],3)
[1, 1, 1, 2, 2, 2, 3, 3, 3]

This would return a list where each element is repeated n times in the list, however beware of this

>>> duplicate([[1,2,3]],3)
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

Or, if you want to replicate the list itself:

>>> def duplicate(testList, n):
...     return testList*n
... 
>>> x=[1,2,3]
>>> duplicate(x,3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

The testList*n will create a new list consisting of testList n number of times, this is equivalent of (testList + testList + ....) n times

Georgy
  • 12,464
  • 7
  • 65
  • 73
dnit13
  • 2,478
  • 18
  • 35
3

To get the same output as your own code, range and extend will still work:

def duplicate(testList, n):
    x = len(testList)
    new_list = []
    for j in range(x):
        new_list.extend(testList[j] for _ in range(n))
    return new_list


print(duplicate([1, 2, 3], 3))
# [1, 1, 1, 2, 2, 2, 3, 3, 3]

Which could simply become a list comprehension:

def duplicate(testList, n):
    return [ele for ele in testList for _ in range(n)]

If you are not seeing output for any of the code then you are most likely running from an IDE and not printing the function return i.e print(duplicate([1, 2, 3], 3)).

Lastly if you were to index and use two loops and use append like your own code, you would use 2 range loops, there is no need for a while loop at all:

def duplicate(testList, n):
    x = len(testList)
    new_list = []
    for j in range(x):
        for _ in range(n):
            new_list.append(testList[j])
    return new_list

If you want [1, 2, 3, 1, 2, 3, 1, 2, 3] and you have to use loops, use a range loop and extend:

def duplicate(testList, n):
    new_list = [] 
    for i in range(n):
        new_list.extend(testList)
    return new_list
Georgy
  • 12,464
  • 7
  • 65
  • 73
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
2

Assuming sortable elements within the list, what about:

a = [1,2,3]
num_repeats = 3

a_expanded = a * num_repeats
a_expanded.sort()
NickBraunagel
  • 1,559
  • 1
  • 16
  • 30
0

You should actually switch the for loop and the while loop and remember to initialize y each time you loop. Compare the variable y in your code and mine, and you will see that you actually want it to go 0,1,2,0,1,2.. so that it will go round and round and append the correct index.

def duplicate(testList, n):
    x = len(testList)
    newList=[]
    for z in range (0, n):
        y = 0
        while y < x:
            newList.append(testList[y])
            y = y + 1
    return newList

print duplicate([1,2,3], 3)
ergonaut
  • 6,929
  • 1
  • 17
  • 47