-5

I want to looping until it meets the condition. In this case i want to continue till List_list looks like

["one","one","two","two","three","three","four","four","five","five","six","seven","eight","nine","ten"]  


lst =["one","two","three","four","five","six","seven","eight","nine","ten”]
List_list = list()    
for rn in lst:
    List_list.append(rn)
    if 15 == len(List_list):
        break
Royal
  • 218
  • 2
  • 17
  • 1
    use a while loop instead and count till 15. Alternate, use for loop with range of 15 and iterate thru the loop with mod 10 (i % 10) will give you the position of the index in the list. That way you can get the position and append. – Joe Ferndz Nov 24 '20 at 23:36
  • 1
    Your if statement will never be met as lst has only 10 items. – Joe Ferndz Nov 24 '20 at 23:37
  • Please, in the future don't change the question once it's been answered. You have invalidated most of the answers here by your edit. If this question gets deleted, you are welcome to post the last question again. But this time, please, provide more details (I understood what you really wanted only after reading the comments under the accepted answer). And as the original question has several duplicates, you don't have to post it again. See, for example: [Repeat list to max number of elements](https://stackoverflow.com/q/39863250/7851470). – Georgy Dec 09 '20 at 10:13

6 Answers6

3

Ask #2:

Solution to repeat first 5 items, then single instance of next 5 items

lst =["one","two","three","four","five","six","seven","eight","nine","ten"]
List_list = []
for i in range(10):
    List_list.append(lst[i])
    if i < 5:
        List_list.append(lst[i])

print (List_list)

The output of this will be:

['one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

If you are looking for a single line answer using list comprehension, then you can use this.

List_list = [y for x in lst[:5] for y in [x,x]] + [x for x in lst[5:]]
print (List_list)

Output is the same:

['one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

Ask #1:

Solution for earlier question: Add 15 items to a list: All 10 items from original list + first from original list

You can do something as simple as this:

List_lst = lst + lst[:5]
print (List_lst)

If you still insist on using a for loop and you want 15 items, then do this and it will give you same output.

List_list = list()
for i in range(15):
    List_list.append(lst[i%10])
    
print (List_list)

A list comprehension version of this will be:

List_list = [lst[i%10] for i in range(15)]

print (List_list)

If you want to fix your code with a while loop, see the details below.

Convert the for loop to while True:. Start iterating using a counter i and check for mod of 10 to get the position to be inserted.

lst = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]

List_list = list()
i = 0
while True:
    List_list.append(lst[i%10])
    i+=1
    if len(List_list) == 15:
        break

print (List_list)

This will result in

["one", "two", "three",  "four",  "five", "six", "seven", "eight", "nine", "ten", "one", "two", "three",  "four",  "five"]  
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • Can you do it so it looks like this ["one","one","two","two","three","three","four","four","five","five","six","seven","eight","nine","ten"] – Royal Nov 25 '20 at 22:07
  • 1
    yea. thats easy. let me get you a version that works like this. – Joe Ferndz Nov 25 '20 at 22:16
  • 1
    I strongly recommend that you try out yourself and see if one of these programs can be expanded to get you the result. For now, I have updated the answer to give you the desired results. – Joe Ferndz Nov 25 '20 at 22:25
  • it not iterating as much as I want it tho :( – Royal Nov 25 '20 at 22:59
  • what is not iterating? Can you explain the problem so I can help you get a resolution. – Joe Ferndz Nov 25 '20 at 23:07
  • My final goal is to evenly distribute lst for any number. For instance sometimes it could be 21 so the list should look like ['one','one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'six', 'seven', 'seven', 'eight', 'eight', 'nine', 'nine', 'ten', 'ten'] , for 15 ['one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] – Royal Nov 25 '20 at 23:38
  • Post this as a separate question and send me the link. I will provide you an answer. This needs a new algorithm. You need to calculate the times you need to iterate. It will be something like this ... `x = int(max_number / len(lst)); y = int(max_number % len(lst)); List_list = x*lst + lst[:y] ` – Joe Ferndz Nov 26 '20 at 00:44
  • 1
    https://stackoverflow.com/questions/61546753/how-to-evenly-distribute-values-in-array-python @Joe Ferndz – Royal Nov 26 '20 at 16:29
3

This seems like a simple modulo 10 loop...

lst =["01.one","02.two","03.three","04.four","05.five","06.six","07.seven","08.eight","09.nine","10.ten"]
[w[3:] for w in sorted([lst[n%10] for n in range(15)])]

output

['one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

frankr6591
  • 1,211
  • 1
  • 8
  • 14
1

Use itertools.cycle:

from itertools import cycle

lst = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "five": 5,
    "six": 6,
    "seven": 7,
    "eight": 8,
    "nine": 9,
    "ten": 10
}

def get_lst(times):
    output = []
    for item in cycle(lst):
        output.append(item)
        if len(output) >= times:
            break
    return sorted(output, key=lambda i: lst[i])

print(get_lst(10))
-> ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

print(get_lst(11))
-> ['one', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
Deneb
  • 981
  • 2
  • 9
  • 25
  • Can I append it like this ['one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] – Royal Nov 26 '20 at 07:56
  • Yes, you use `output.append(item)` two times and set the `len(output)` to `>= 30`. – Deneb Nov 26 '20 at 09:47
  • didnt quite get it, Can you edit your answer please ? – Royal Nov 26 '20 at 16:34
  • Do you want the same *exact* output as the one you mentioned above? – Deneb Nov 26 '20 at 17:14
  • It doesn't have to be 15 every time maybe sometime it will be 10 or 12 or even 30. I want it to work every instance. So if its 10 then ["one","two","three","four","five","six","seven","eight","nine","ten”], if 11 then ["one","one","two","three","four","five","six","seven","eight","nine","ten”], or if its 31 then every item 3 times and first or last item 4 times – Royal Nov 26 '20 at 17:26
  • 1
    @EegiiEnkhtaivan Check updated answer. – Deneb Nov 26 '20 at 17:42
1

EDIT 2: Complemented the answer for the updated question.

Using itertools from Python Standard Library, you can do it in three steps (steps 1 and 2 can be combined):

  1. Use the cycle function to create an infinite iterator of the original list.
  2. Use the islice function to get the first 15 elements from the infinite iterator.
  3. Sort items in the resultant list by the position in the original list.
from itertools import cycle, islice

lst = ["one","two","three","four","five","six","seven","eight","nine","ten"]

infinite_lst = cycle(lst)
List_list = list(islice(infinite_lst, 15))
List_list.sort(key=lst.index)

print(List_list)

And here you have:

['one', 'one', 'two', 'two', 'three', 'three', 'four', 'four', 'five', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
leogama
  • 898
  • 9
  • 13
1
["one","two","three","four","five","six","seven","eight","nine","ten","one","two","three","four","five"]


lst =["one","two","three","four","five","six","seven","eight","nine","ten"]
List_list = [] 
length = 0
# Respect the looping statement 
while length<15:
    List_list.append(lst[length%10])
    length+=1
print(List_list)
#Output ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'one', 'two', 'three', 'four', 'five']

ombk
  • 2,036
  • 1
  • 4
  • 16
0

I agree that the second code in this answer is not more complicated as it is much easier to replicate for any other code. The best programmers create code which can be reproduced in any circumstance the best.