when you use while True: the code inside it will run forever until you break

so in your code, I think you need to do this:

test_list = [ 1, 6, 3, 5, 3, 4 ]
alert = []
for i in test_list:
    if not i in alert:
        print('exists')
        alert.append(i)

EDIT: if you want to run it forever:

test_list = [ 1, 6, 3, 5, 3, 4 ]
alert = []
while True:
    for i in test_list:
        if not i in alert:
            print('exists')
            alert.append(i)
Answer from Mhmd Admn on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_lists_loop.asp
Python - Loop Lists
Print all items by referring to ... » · The iterable created in the example above is [0, 1, 2]. You can loop through the list items by using a while loop....
🌐
GeeksforGeeks
geeksforgeeks.org › python › loop-through-a-list-using-while-loop-in-python
Loop Through a List using While Loop in Python - GeeksforGeeks
July 23, 2025 - In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more ...
🌐
Python.org
discuss.python.org › python help
Python learning while loop list - Python Help - Discussions on Python.org
September 19, 2022 - Hello everyone, I am having a hard time understanding while loop when it comes to a list. Using the while loop: List all the presidents whose names do not contain the letter ‘w’ listUSPresidents=[“George Washington”,“John Adams”,“Thomas Jefferson”,“James Madison”,“James Monroe”,“John Quincy Adams”,“Andrew Jackson”,“Martin Van Buren”,“William H. Harrison”,“John Tyler”,“James K. Polk”,“Zachary Taylor”,“Millard Fillmore”,“Franklin Pierce”,“James Buchanan”,“Abraham Lincoln”,“Andrew Johnson”,“Ulys...
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC210 › loops › while.html
while loops and dynamic lists
Methods are easy to recognize in the Python program, because they are invoked using a special 'dot' syntax that take the form ... Here is the code for a program that constructs a list of all of the prime factors of a given positive integer. divisors = [] N = int(input('Enter N: ')) # 2 is the first divisor to try d = 2 while N > 1: if N % d == 0: # If d divides evenly into N, add it to the list of # divisors and divide it out of N.
🌐
Educative
educative.io › answers › how-can-we-loop-through-a-list-using-while-loop-in-python
How can we loop through a list using while loop in Python
Line 5: We create a variable i, to represent the index numbers of the items of the list starting with index 0 (i=0). Line 8: We create a while loop and use the len() function to iterate over the list by referring to all the index numbers of each items of the list.
🌐
Python Examples
pythonexamples.org › python-list-while-loop
Python List While Loop
In general, the syntax to iterate over a list using while loop is · index = 0 while index < len(myList): element = myList[index] #statement(s) index += 1 ... element contains value of the this element in the list. For each iteration, next element in the list is loaded into this variable with ...
🌐
LearnPython.com
learnpython.com › blog › python-list-loop
7 Ways to Loop Through a List in Python | LearnPython.com
So, a while loop executes until a certain condition is met. In the code below, that condition is the length of the list; the i counter is set to zero, then it adds 1 every time the loop prints one item in the list.
Find elsewhere
🌐
Real Python
realpython.com › videos › while-loops-and-lists
While Loops and Lists (Video) – Real Python
In this lesson you’ll learn how to iterate over a list using a while-loop. The code is debugged in a live session in the video.
Published   March 1, 2019
Top answer
1 of 4
15

what you are looking for is:

for i, elem in enumerate(foo):
    #i will equal the index
    #elem will be the element in foo at that index
    #etc...

the enumerate built-in takes some sequence (like a list, or a generator), and yields a tuple, the first element of which contains the iteration number, and the second element of which contains the value of the sequence for that iteration.

Since you specifically ask about an "unknown number of indexes", I also want to clarify by adding that enumerate works lazily. Unlike len, which needs to calculate/evaluate an entire sequence in advance (assuming that you are working with something more complicated than a simple list), and would thus fail for an infinite list, and take an indeterminate amount of time for some arbitrary generator function (which would then need to be run again, possibly leading to side effects), enumerate only evaluates the next sequence in the list. This can be shown using the fact that it will perform as expected on the easiest to make example of a sequence with an unknown number of entries: an infinite list:

import itertools
for i, elem in enumerate(itertools.cycle('abc')):
    #This will generate -
    # i = 0, elem = 'a'
    # i = 1, elem = 'b'
    # i = 2, elem = 'c'
    # i = 3, elem = 'a'
    # and so on, without causing any problems.

EDIT - in response to your comments:

Using for ... enumerate ... is inherently more expensive than just using for (given that you have the overhead of doing an additional enumerate). However, I would argue that if you are tracking the indexes of your sequence elements, that this tiny bit of overhead would be a small price to pay for the very pythonic presentation it affords (that is to say, i=0; i += 1 is a very C-style idiom).

After doing a little bit of searching, I dug up (and then agf corrected the url for) the source code for the enumerate function (and additionally, the reversed function). It's pretty simple to follow, and it doesn't look very expensive at all.

2 of 4
8

if you have a python list, you can iterate through the list with a for loop:

for i in list

if you have to use the while loop, you can try

i=0
while i<len(foo):
  ...
  i+=1
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
The range(len(list)) generates indices from 0 to the length of the list minus 1. In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied.
Published   June 7, 2017
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_lists.htm
Python - Loop Lists
Following is the basic syntax to ... end = ' ') ... A while loop in Python is used to repeatedly execute a block of code as long as a specified condition evaluates to "True"....
🌐
Dot Net Perls
dotnetperls.com › while-python
Python - while Loop Examples - Dot Net Perls
We can use functions for the condition, ... functions control both parts of the while-loop. The text "Hello friend" is printed. ... A list can be looped over with for or while....
🌐
Medium
nidhiashtikar.medium.com › python-day-22-using-a-while-loop-with-lists-and-dictionaries-010e742e56db
Python- Day 22- Using a While Loop with Lists and Dictionaries | by Nidhi Ashtikar | Medium
October 25, 2024 - Python- Day 22- Using a While Loop with Lists and Dictionaries Handling Multiple User Inputs: While loops can collect and manage multiple inputs by storing them in lists or dictionaries. Unlike for …
🌐
Reddit
reddit.com › r/learnpython › can someone elaborate on this point about while loops? (from python crash course book)
r/learnpython on Reddit: Can someone elaborate on this point about while loops? (from Python Crash Course book)
July 11, 2017 -

I was going through the "Python Crash Course" book by Eric Matthes, and in a chapter dedicated to "User Input and While Loops" read the following:

A for loop is effective for looping through a list, but you shouldn’t modify a list inside a for loop because Python will have trouble keeping track of the items in the list. To modify a list as you work through it, use a while loop

Is there any benefit on using a while loop to traverse a list instead of a for loop? Other than the while loop "knows" if there are any elements remaining in said list. Oh, forgot to mention, the examples involve moving items from one list to another, until the first one is empty.

Also, what is your opinion about this book? I have found it amazing so far, and the fact it includes a couple of full-fledged projects motivates me to go thorugh it and research about any concept I may not feel comfortable with.

🌐
Codecademy
codecademy.com › learn › dspath-python-lists-and-loops › modules › dspath-loops › cheatsheet
Python Lists and Loops: Python Loops Cheatsheet | Codecademy
To interrupt a Python program that is running forever, press the Ctrl and C keys together on your keyboard. In Python, a while loop will repeatedly execute a code block as long as a condition evaluates to True.
🌐
GitHub
hplgit.github.io › primer.html › doc › pub › looplist › ._looplist-bootstrap002.html
Loops and lists
In each pass of the loop, the variable C refers to an element in the list, starting with degrees[0], proceeding with degrees[1], and so on, before ending with the last element degrees[n-1] (if n denotes the number of elements in the list, len(degrees)). The for loop specification ends with a colon, and after the colon comes a block of statements that does something useful with the current element. Each statement in the block must be indented, as we explained for while loops.
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
This method allows us to access each element in the list directly. We can access all elements using for loop and in keyword to traverse all elements ... a = [1, 3, 5, 7, 9] # On each iteration val # represents the current item/element for val ...
Published   December 27, 2025
🌐
StrataScratch
stratascratch.com › blog › looping-through-lists-in-python
Looping Through Lists in Python: A Comprehensive Tutorial - StrataScratch
April 9, 2025 - First, never change a list while looping it over. That’s a recipe for bugs. Always work with a copy (or create a new list to work from). Second, when tracking positions or working with multiple lists, use tools such as `enumerate()` or `zip()`. They’re also more than fancy—they prevent index headaches. And if your loop can be expressed in a single line, don’t overthink it. Python ...