Loop Through a List using While Loop in Python Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 commonly used for loop. In this article, we'll explore four simple examples of how to loop through a list using the while loop in Python. Loop Through A List Using While Loop In PythonBelow, are the ways to Loop Through A List Using While Loop In Python. Basic List IterationLooping with a ConditionModifying List ElementsUser Input in LoopBasic List IterationIn this example, we initialize an index variable to 0 and use a while loop to iterate through the list until the index exceeds the length of the list. The value at the current index is printed, and the index is incremented in each iteration Python3 # Basic List Iteration my_list = [1, 2, 3, 4, 5] index = 0 while index < len(my_list): print(my_list[index]) index += 1 Output1 2 3 4 5Loop Through A List Using While Loop by Looping with a ConditionThis example demonstrates how to loop through a list with a condition. Here, we print only the elements of the list that have a length greater than 5 characters. Python3 #Looping with a Condition fruits = ["apple", "banana", "orange", "grape"] index = 0 while index < len(fruits): if len(fruits[index]) > 5: print(fruits[index]) index += 1 Outputbanana orangeLoop Through A List Using While Loop by Modifying List ElementsIn this example, the while loop is used to modify the elements of the list. Each element is multiplied by 2, resulting in a list where every value has been doubled Python3 # Modifying List Elements numbers = [1, 2, 3, 4, 5] index = 0 while index < len(numbers): numbers[index] *= 2 # Multiply each element by 2 index += 1 print(numbers) Output[2, 4, 6, 8, 10]Loop Through A List Using While Loop by User Input in LoopHere, the while loop is used to continuously prompt the user for input until they enter "done." The entered numbers are converted to integers and added to a list. Python3 # User Input in Loop user_list = [] user_input = input("Enter a number (type 'done' to finish): ") while user_input.lower() != 'done': user_list.append(int(user_input)) user_input = input("Enter another number (type 'done' to finish): ") print("User's List:", user_list) Output Enter a number (type 'done' to finish): 3Enter another number (type 'done' to finish): 3Enter another number (type 'done' to finish): doneUser's List: [3, 3] Comment More infoAdvertise with us Next Article Loop Through a List using While Loop in Python neeraj3304 Follow Improve Article Tags : Python Python Programs python Practice Tags : pythonpython Similar Reads How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth 2 min read How to Emulate a Do-while loop in Python? We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop. Do while loop is a type of control looping statement that can run any statement until th 3 min read Get User Input in Loop using Python In Python, for and while loops are used to iterate over a sequence of elements or to execute a block of code repeatedly. When it comes to user input, these loops can be used to prompt the user for input and process the input based on certain conditions. In this article, we will explore how to use fo 3 min read How to Kill a While Loop with a Keystroke in Python? While loops are used to repeatedly execute a block of code until a condition is met. But what if you want the user to stop the loop manually, say by pressing a key? Then you can do so by pressing a key. This article covers three simple ways to break a while loop using a keystroke:Using KeyboardInter 2 min read Python Program to Find the Sum of Natural Numbers Using While Loop Calculating the Sum of N numbers in Python using while loops is very easy. In this article, we will understand how we can calculate the sum of N numbers in Python using while loop.Calculate Sum of Natural Numbers in Python Using While LoopBelow are some of the examples by which we can see how we can 2 min read List As Input in Python in Single Line Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com 3 min read Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the 3 min read Tokenizing Strings in List of Strings - Python The task of tokenizing strings in a list of strings in Python involves splitting each string into smaller units, known as tokens, based on specific delimiters. For example, given the list a = ['Geeks for Geeks', 'is', 'best computer science portal'], the goal is to break each string into individual 2 min read How to Modify a List While Iterating in Python Modifying a list while iterating can be tricky but there are several ways to do it safely. One simple way to modify a list while iterating is by using a for loop with the index of each item. This allows us to change specific items at certain positions.Pythona = [1, 2, 3, 4] for i in range(len(a)): i 2 min read Add items to List While Iterating - Python Python has many ways to append to a list while iterating. List comprehension is a compact and efficient way to append elements while iterating. We can use it to build a new list from an existing one or generate new values based on a condition.Pythona = [1, 2, 3] a += [i + 4 for i in range(3)] print( 2 min read Like