Loop Through a List using While Loop in Python
Last Updated :
07 Feb, 2024
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 Python
Below, are the ways to Loop Through A List Using While Loop In Python.
- Basic List Iteration
- Looping with a Condition
- Modifying List Elements
- User Input in Loop
Basic List Iteration
In 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
Loop Through A List Using While Loop by Looping with a Condition
This 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
Loop Through A List Using While Loop by Modifying List Elements
In 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)
Loop Through A List Using While Loop by User Input in Loop
Here, 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): 3
Enter another number (type 'done' to finish): 3
Enter another number (type 'done' to finish): done
User's List: [3, 3]
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