0

I need some kind of function or little tip for my problem.

So I got a list let's say [1,2,3,4] but I need this array to be longer with the same elements repeated so let's say I need an array of length 10 so it becomes: [1,2,3,4,1,2,3,4,1,2]

So I need to extend the list with the same values as in the list in the same order

returnString = the array or string to return with extended elements
array = the basic array which needs to be extended
length = desired length

EDIT:

returnString = ""
array = list(array)
index = 0
while len(str(array)) != length:
    if index <= length:
        returnString += array[index]
        index += 1
    else:
        toPut = index % length
        returnString.append(array[toPut])
        index += 1
return returnString
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Divide the new length by the length of the list, and multiply the list that many times. If it doesn't divide equally, concatenate enough elements to get to the desired length. – Barmar Sep 10 '20 at 20:27
  • @martineau Sorry, I edited the post! I hope it's clear, my code could be cleaner I know that! –  Sep 10 '20 at 20:34
  • Why are you converting the array to a string? – Barmar Sep 10 '20 at 20:36
  • Why are you using strings at all, the question is about a list. – Barmar Sep 10 '20 at 20:36
  • @Barmar as my output wasn't correct I tried to use other ways. –  Sep 10 '20 at 20:37

7 Answers7

5

This is simple with itertools.cycle and itertools.islice:

from itertools import cycle, islice

input = [1, 2, 3, 4]
output = list(islice(cycle(input), 10))

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
Jab
  • 26,853
  • 21
  • 75
  • 114
  • I see that you can use it for in-place extension also `input.extend(islice(cycle(input), 6))` (see also my answer where I `append` to a list using `cycle`, but I didn't think of `islice` until you mentioned it) – alani Sep 10 '20 at 20:39
  • Absolutely, although I don't think OP wanted to do that – Jab Sep 10 '20 at 20:41
  • Question was a little ambiguous because it says "So I need to extend the list..." but then has some code involving a new list. – alani Sep 10 '20 at 21:01
3

You can use itertools.cycle to iterate repeatedly over the list, and take as many values as you want.

from itertools import cycle

lst = [1, 2, 3, 4]
myiter = cycle(lst)
print([next(myiter) for _ in range(10)])
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]

You can also use it to extend the list (it doesn't matter if you append to the end while you are iterating over it, although removing items would not work).

from itertools import cycle

lst = [1, 2, 3, 4]
myiter = cycle(lst)
for _ in range(6):
    lst.append(next(myiter))
print(lst)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
alani
  • 12,573
  • 2
  • 13
  • 23
2

One way could be:

Iterate over the desired length - len(x_lst), So you have 10 - 4 = 6 (new elements to be added). Now since the list element should repeat, you can append the x_lst elements on the go by the indices (0,1,2,3,4,5).

x = [1,2,3,4]

length = 10

for i in range(length - len(x)):
    x.append(x[i])

print(x)

OUTPUT:

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1

Try this:

n = 10
lst =[1,2,3,4]
new_lst = [lst[i%len(lst)] for i in range(n)]
print(new_lst)

Output:

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
dimay
  • 2,768
  • 1
  • 13
  • 22
0
NumOfValues = int(input("Number of Values: "))
List1 = [1,2,3,4]
List2 = []
Count = 0
while len(List2) < NumOfValues:
    List2.append(List1[Count])
    Count += 1
    if Count > len(List1) - 1:
        Count = 0
print(List2)
Evorage
  • 493
  • 3
  • 15
0

I will show a tip to you:

If you have this array [1,2,3,4] so you can create a separated newArray that get this value and fill the newArray with this repeated values. How? Loop! I think for can do this to you, just point the array and newArray to it knows which it will fill.

devs121
  • 49
  • 8
0

First multiply the list by the number of times it needs to be repeated. If that's not the desired length, extend it with the appropriate slice of the list.

old_len = len(original)
new_len = 10
result = original * new_len // old_len
if new_len % old_len != 0:
    result += original[:new_len % old_len]
Barmar
  • 741,623
  • 53
  • 500
  • 612