Skip to content
Advertisement

How to continue for loop until it meets condition? [closed]

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

JavaScript

Advertisement

Answer

Ask #2:

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

JavaScript

The output of this will be:

JavaScript

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

JavaScript

Output is the same:

JavaScript

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:

JavaScript

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

JavaScript

A list comprehension version of this will be:

JavaScript

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.

JavaScript

This will result in

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement