B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Loop Through List Items in Python | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Looping Through List Items in Python

To work with all items in a Python list, we use loops. Let’s explore for loop, while loop, enumerate(), and range().

Python list के सभी items को access करने के लिए हम loop का use करते हैं। आइए for loop, while loop, enumerate(), और range() को समझते हैं।

1. Using for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
Output:
apple
banana
cherry
2. Using for loop with index
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
    print(i, fruits[i])
Output:
0 apple
1 banana
2 cherry
3. Using while loop
fruits = ["apple", "banana", "cherry"]
i = 0
while i < len(fruits):
    print(fruits[i])
    i += 1
Output:
apple
banana
cherry
4. Using enumerate()
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print(index, fruit)
Output:
0 apple
1 banana
2 cherry
5. Using list comprehension (advanced)
fruits = ["apple", "banana", "cherry"]
[print(fruit.upper()) for fruit in fruits]
Output:
APPLE
BANANA
CHERRY