Movatterモバイル変換


[0]ホーム

URL:


— FREE Email Series —

🐍 Python Tricks 💌

Python Tricks Dictionary Merge

🔒 No spam. Unsubscribe any time.

Browse TopicsGuided Learning Paths
Basics Intermediate Advanced
aialgorithmsapibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnewsnumpyprojectspythonstdlibtestingtoolsweb-devweb-scraping

Table of Contents

Recommended Course

Python "for" Loops (Definite Iteration)

For Loops in Python (Definite Iteration)

16m · 4 lessons

Python for Loops: The Pythonic Way

Python for Loops: The Pythonic Way

byLeodanis Pozo RamosPublication date Feb 23, 2026Reading time estimate 36mintermediatebest-practicespython

Table of Contents

Remove ads

Recommended Course

For Loops in Python (Definite Iteration)(16m)

Python’sfor loop allows you to iterate over the items in a collection, such as lists, tuples, strings, and dictionaries. Thefor loop syntax declares a loop variable that takes each item from the collection in each iteration. This loop is ideal for repeatedly executing a block of code on each item in the collection. You can also tweakfor loops further with features likebreak,continue, andelse.

By the end of this tutorial, you’ll understand that:

  • Python’sfor loop iterates over items in a data collection, allowing you to execute code for each item.
  • Toiterate from0 to10, you use thefor index in range(11): construct.
  • Torepeat code a number of times without processing the data of an iterable, use thefor _ in range(times): construct.
  • To doindex-based iteration, you can usefor index, value in enumerate(iterable): to access both index and item.

In this tutorial, you’ll gain practical knowledge of usingfor loops to traverse various collections and learn Pythonic looping techniques. You’ll also learn how to handle exceptions and use asynchronous iterations to make your Python code more robust and efficient.

Get Your Code:Click here to download the free sample code that shows you how to use for loops in Python.

Take the Quiz: Test your knowledge with our interactive “Python for Loops: The Pythonic Way” quiz. You’ll receive a score upon completion to help you track your learning progress:


Python for Loops: The Pythonic Way

Interactive Quiz

Python for Loops: The Pythonic Way

In this quiz, you'll test your understanding of Python's for loop. You'll revisit how to iterate over items in a data collection, how to use range() for a predefined number of iterations, and how to use enumerate() for index-based iteration.

Getting Started With the Pythonfor Loop

In programming,loops arecontrol flowstatements that allow you to repeat a given set of operations a number of times. In practice, you’ll find two main types of loops:

  1. for loops are mostly used to iterate aknown number of times, which is common when you’re processing datacollections with a specific number of data items.
  2. while loops are commonly used to iterate anunknown number of times, which is useful when the number ofiterations depends on a given condition.

Python has both of these loops and in this tutorial, you’ll learn aboutfor loops. In Python, you’ll generally usefor loops when you need to iterate over the items in a data collection. This type of loop lets you traverse different data collections and run a specific group of statementson orwith each item in the input collection.

In Python,for loops arecompound statements with aheader and acode block that runs a predefined number of times. The basic syntax of afor loop is shown below:

Python Syntax
forvariableiniterable:<body>

In this syntax,variable is the loopvariable. In each iteration, this variable takes the value of the current item initerable, which represents the data collection you need to iterate over. The loop body can consist of one or more statements that must be indented properly.

Here’s a more detailed breakdown of this syntax:

  • for is the keyword that initiates the loop header.
  • variable is a variable that holds the current item in the inputiterable.
  • in is a keyword that connects the loop variable with the iterable.
  • iterable is a data collection that can be iterated over.
  • <body> consists of one or more statements to execute in each iteration.

Here’s a quick example of how you can use afor loop to iterate over alist:

Python
>>>colors=["red","green","blue","yellow"]>>>forcolorincolors:...print(color)...redgreenblueyellow

In this example,color is the loop variable, while thecolors list is the target collection. Each time through the loop,color takes on a successive item fromcolors. In this loop, the body consists of a call toprint() that displays the value on the screen. This loop runs once for each item in the target iterable. The way the code above is written is thePythonic way to write it.

However, what’s aniterable anyway? In Python, aniterable is an object—often a data collection—that can be iterated over. Common examples of iterables in Python includelists,tuples,strings,dictionaries, andsets, which are all built-in data types. You can also have custom classes that support iteration.

Note: Python has both iterables and iterators. Iterables support theiterable protocol consisting of the.__iter__()special method. Similarly, iterators support theiterator protocol that’s based on the.__iter__() and.__next__() special methods.

Both iterables and iterators can be iterated over. All iterators are iterables, but not all iterables are iterators. Python iterators play a fundamental role infor loops because they drive the iteration process.

A deeper discussion on iterables and iterators is beyond the scope of this tutorial. However, to learn more about them, check out theIterators and Iterables in Python: Run Efficient Iterations tutorial.

You can also have a loop with multiple loop variables:

Python
>>>points=[(1,4),(3,6),(7,3)]>>>forx,yinpoints:...print(f"{x= } and{y= }")...x = 1 and y = 4x = 3 and y = 6x = 7 and y = 3

In this loop, you have two loop variables,x andy. Note that to use this syntax, you just need to provide a tuple of loop variables. Also, you can have as many loop variables as you need as long as you have the correct number of items tounpack into them. You’ll also find this pattern useful when iterating overdictionary items or when you need to doparallel iteration.

Sometimes, the input iterable may be empty. In that case, the loop will run its header once but won’t execute its body:

Python
>>>foritemin[]:...print(item)...

In this example, the target iterable is an empty list. The loop checks whether the iterable has items. If that’s the case, then the loop runs once for each item. If the iterable has no items, then the loop body doesn’t run, and the program’s execution flow jumps onto the statement after the loop.

Now that you know the basic syntax offor loops, it’s time to dive into some practical examples. In the following section, you’ll learn how to usefor loops with the most common built-in data collections in Python.

Traversing Built-in Collections in Python

When writing Python code, you’ll often need to iterate over built-in data types such as lists, tuples, strings, numeric ranges, dictionaries, and sets. All of them support iteration, and you can feed them into afor loop. In the next sections, you’ll learn how to tackle this requirement in aPythonic way.

Sequences: Lists, Tuples, Strings, and Ranges

When it comes to iterating oversequence data types like lists, tuples, strings, and ranges, the iteration happens in the same order that the items appear in thesequence. Consider the following example where you iterate over the numbers in a list:

Python
>>>numbers=[1,2,3,4]>>>fornumberinnumbers:...print(number)...1234

In this example, the iteration goes through the list in the definition order, starting with1 and ending with4. Note that to iterate over a sequence in Python, you don’t need to be aware of the index of each item as in other languages where loops often rely on indices.

Often, you use plural nouns to name lists. This naming practice allows you to use singular nouns as the loop variable, making your code descriptive and readable.

Note: To learn more about using lists, check outPython’slist Data Type: A Deep Dive With Examples.

You’ll note the same behavior with other built-in sequences:

Python
>>>person=("Jane",25,"Python Dev","Canada")>>>forfieldinperson:...print(field)...Jane25Python DevCanada>>>text="abcde">>>forcharacterintext:...print(character)...abcde>>>forindexinrange(5):...print(index)...01234

In these examples, you iterate over a tuple, string, and numeric range. Again, the loop traverses the sequence in the order of definition.

Note: For more information about tuples, strings, and ranges, you can check out the following tutorials:

Tuples are often used to represent rows of data. In the example above, theperson tuple holds data about a person. You can iterate over each field using a readable loop.

When it comes to iterating over string objects, thefor loop lets you process the string on a character-by-character basis. Finally, iterating over a numeric range is sometimes a requirement, especially when you need toiterate a given number of times and need control over the consecutive index.

Collections: Dictionaries and Sets

When traversing dictionaries with afor loop, you’ll find that you can iterate over the keys, values, and items of thedictionary at hand.

Note: To learn more about dictionary iteration, check out theHow to Iterate Through a Dictionary in Python tutorial.

You’ll have two different ways to iterate over the keys of a dictionary. You can either use:

  1. The dictionary directly
  2. The.keys()method

The following examples show how to use these two approaches:

Python
>>>students={..."Alice":89.5,..."Bob":76.0,..."Charlie":92.3,..."Diana":84.7,..."Ethan":88.9,...}>>>forstudentinstudents:...print(student)...AliceBobCharlieDianaEthan>>>forstudentinstudents.keys():...print(student)...AliceBobCharlieDianaEthan

In these examples, you first iterate over the keys of a dictionary using the dictionary directly in the loop header. In the second loop, you use the.keys() method to iterate over the keys. While both approaches are equivalent, the first one is more commonly used, whereas the second might be more readable and explicit.

In both loops, you can access the dictionary values using the keys:

Python
>>>forstudentinstudents:...print(student,"->",students[student])...Alice -> 89.5Bob -> 76.0Charlie -> 92.3Diana -> 84.7Ethan -> 88.9

To access the values in this type of iteration, you can use the original dictionary and a key lookup operation, as shown in the highlighted line.

You can use the.values() method to feed thefor loop when you need to iterate over the values of a dictionary:

Python
>>>teams={..."Colorado":"Rockies",..."Chicago":"White Sox",..."Boston":"Red Sox",..."Minnesota":"Twins",..."Milwaukee":"Brewers",..."Seattle":"Mariners",...}>>>forteaminteams.values():...print(team)...RockiesWhite SoxRed SoxTwinsBrewersMariners

The.values() method lets you traverse the values in the target dictionary. In this example, you iterate over team names one by one. Note that when you use the.values() method, you can’t access the dictionary keys.

Finally, iterating over both keys and values in a Python dictionary is a common requirement. In this case, the recommended and most Pythonic approach is to use the.items() method in afor loop like the following:

Python
>>>forplace,teaminteams.items():...print(place,"->",team)...Colorado -> RockiesChicago -> White SoxBoston -> Red SoxMinnesota -> TwinsMilwaukee -> BrewersSeattle -> Mariners

When iterating over keys and values this way, you typically use a tuple of loop variables. The first variable will get the key, while the second will get the associated value. In this example, you have theplace andteam variables, which make the code clear and readable.

When it comes to iterating oversets, you only have to keep in mind that sets are unordered data types. This means that looping in order isn’t guaranteed:

Python
>>>tools={"Django","Flask","pandas","NumPy"}>>>fortoolintools:...print(tool)...NumPyFlaskpandasDjango

As you can see, the loop goes through the elements of your set in a different order than they were inserted. So, you can’t rely on the order of the elements when traversing sets in Python.

Using Advancedfor Loop Syntax

The Pythonfor loop has some advanced features that make it flexible and powerful. These features can be helpful when you need to fine-tune the loop to meet specific execution flows. These features include thebreak andcontinue statements and theelse clause, which you’ll learn about in the following sections.

You’ll also learn thatfor loops can be nested inside one another. This feature can be pretty useful in situations where you need to iterate over nested data structures like lists of lists.

Thebreak Statement

Thebreak statement immediately exits the loop and jumps to the first statement after the loop. For example, say that you want to write a loop to determine whether a number is in a list. To avoid unnecessary work, the loop should terminate once it finds the target value. You can do this with thebreak statement:

Python
>>>numbers=[1,3,5,7,9]>>>target=5>>>fornumberinnumbers:...print(f"Processing{number}...")...ifnumber==target:...print(f"Target found{target}!")...break...Processing 1...Processing 3...Processing 5...Target found 5!

In this example, thebreak statement jumps out of the loop as soon as the target number is found. The remaining values,7 and9, aren’t processed. You can think of thebreak statement as a way to short-circuit the loop execution once you’ve gotten the desired result.

It’s important to note that it makes little sense to havebreak statements outside conditionals. Suppose you include abreak statement directly in the loop body without wrapping it in a conditional. In that case, the loop will terminate in the first iteration, potentially without running the entire loop body.

Thecontinue Statement

Thecontinue statement terminates the current iteration and proceeds to the next one. For example, if you have a list of numbers and only want to process the even ones, you can use acontinue statement to skip the odd numbers:

Python
>>>numbers=[1,2,3,4,5,6]>>>fornumberinnumbers:...print(f"{number= }")...ifnumber%2!=0:...continue...print(f"{number} is even!")...number = 1number = 22 is even!number = 3number = 44 is even!number = 5number = 66 is even!

In this example, the code that processes the numbers is only reached if the number is even. Otherwise, thecontinue statement skips that code and jumps right into the next iteration.

Again, it doesn’t make much sense to have acontinue statement without wrapping it in a conditional. If you do so, the code after the statement will be unreachable and never run.

Theelse Clause

In Python,for loops can have anelse clause at the end. Theelse clause will only run if the loop terminates because of the exhaustion of the input iterable. This feature is useful when you have abreak statement that can terminate the loop in certain situations. If the loop doesn’t break, then you can run additional code in theelse clause.

To illustrate, say that you want to continue improving the loop that determines whether a number is in a list. You’d like to explicitly inform the user if the number isn’t in the list. You can do this with theelse clause:

Python
>>>numbers=[1,3,5,7,9]>>>target=42>>>fornumberinnumbers:...print(f"Processing{number}...")...ifnumber==target:...print(f"Target found{target}!")...break...else:...print(f"Target not found{target}")...Processing 1...Processing 3...Processing 5...Processing 7...Processing 9...Target not found 42

Theelse clause won’t run if the loop breaks out with thebreak statement. It only runs if the loop terminates normally, allowing you to inform the user that the target number wasn’t found.

It doesn’t make sense to have anelse clause in a loop that doesn’t have abreak statement. In that case, placing theelse block’s content after the loop—withoutindentation—will work the same and be cleaner.

Nestedfor Loops

You can also have nestedfor loops. In the example below, you create a multiplication table that shows the products of all combinations of integers up to ten usingnested loops. The outer loop iterates over the numbers between1 and10, and the inner loop calculates and prints the products:

Python
>>>fornumberinrange(1,11):...forproductinrange(number,number*11,number):...print(f"{product:>4d}",end="")...print()...   1   2   3   4   5   6   7   8   9  10   2   4   6   8  10  12  14  16  18  20   3   6   9  12  15  18  21  24  27  30   4   8  12  16  20  24  28  32  36  40   5  10  15  20  25  30  35  40  45  50   6  12  18  24  30  36  42  48  54  60   7  14  21  28  35  42  49  56  63  70   8  16  24  32  40  48  56  64  72  80   9  18  27  36  45  54  63  72  81  90  10  20  30  40  50  60  70  80  90 100

In this example, you use two nested loops. Together, they create a two-dimensional multiplication table. First, you loop over the numbers from one up to and including ten. These represent the rows in the table, and you can see those numbers at the beginning of each row.

In the inner loop, you calculate the products for the currentnumber by iterating from thenumber itself up to its tenth multiple. Then, you format each product using the:>4dformat specifier. This ensures the table is nicely aligned. By settingend to an empty string, you skip the newline until the products on the current row are printed. After printing all products for a row, you useprint() withoutarguments to move to the next row.

Exploring Pythonic Looping Techniques

When people switch from other programming languages to Python, they often writefor loops like they did in their previous language. This practice makes Python code look odd and hard to read.

In the following sections, you’ll explore some looping techniques,best practices, and tips that are considered Pythonic. These techniques can make your Python code look clearer, more elegant, and more efficient.

Iterating With Indices: The Pythonic Way

Sometimes, you need to use the indices of items when you iterate over a sequence with a Pythonfor loop. Up to this point, you’ve seen examples where you can access the items but don’t know their corresponding indices.

To get both the item and its index, you can end up writing a loop like the one shown in the following example:

Python
>>>fruits=["orange","apple","mango","lemon"]>>>forindexinrange(len(fruits)):...fruit=fruits[index]...print(index,fruit)...0 orange1 apple2 mango3 lemon

This loop gets the job done, but it’s not as clean or readable as you’d expect from Python code. Fortunately, there’s a better way—the built-inenumerate() function:

Python
>>>forindex,fruitinenumerate(fruits):...print(index,fruit)...0 orange1 apple2 mango3 lemon

Theenumerate()function takes an iterable as an argument and generates tuples of the form(index, item). Note that the loop reads almost like plain English, which makes your code way more Pythonic than the previous version usingrange().

Note: To learn more about working withenumerate(), check out thePythonenumerate(): Simplify Loops That Need Counters tutorial.

Theenumerate() function also takes an optional argument calledstart that lets you tweak the initial value. This feature is useful when you need to create counts. Consider the following example that mimics an option menu for a command-line application:

Python
>>>defdisplay_menu(options):...print("Main Menu:")...forposition,optioninenumerate(options,start=1):...print(f"{position}.{option}")...>>>display_menu(["Open","Save","Settings","Quit"])Main Menu:1. Open2. Save3. Settings4. Quit

In this example, instead of usingenumerate() to produce zero-based indices, you start the count at1. From the end user’s perspective, starting the menu at1 is the natural way to go.

Looping Over Several Iterables in Parallel

Looping through two or more iterables in parallel may be another common task you encounter in Python programming. To do this, you can use the built-inzip() function, which takes two or more iterables andyields tuples that combine items from each iterable.

Note: To learn more aboutzip(), check out theUsing the Python zip() Function for Parallel Iteration tutorial.

Consider the following toy example:

Python
>>>numbers=[1,2,3]>>>letters=["a","b","c"]>>>fornumber,letterinzip(numbers,letters):...print(number,"->",letter)...1 -> a2 -> b3 -> c

In this example, you usezip(numbers, letters) to create an iterator that produces tuples of the form(number, letter). In this case, thenumber values are taken fromnumbers, and theletter values are taken fromletters.

Iterating Over Multiple Iterables Sequentially

There may be times when you need to iterate over multiple iterables sequentially in a single loop. In such cases, you can use thechain() function from Python’sitertoolsmodule.

Note: To learn more about theitertools module and the tools it provides, check out thePythonitertools By Example tutorial.

For example, say that you have several lists of numbers and want to calculate the square of each number in all lists. You can usechain() as follows:

Python
>>>fromitertoolsimportchain>>>first=[7,6,1]>>>second=[4,1]>>>third=[8,0,6]>>>forvalueinchain(first,second,third):...print(value**2)...4936116164036

This loops over all three lists in sequence and prints the square of each value. You can also usechain() to work through a list of lists. Say that you, again, need to process each value in a sequence and calculate its square:

Python
>>>matrix=[...[9,3,8],...[4,5,2],...[6,4,3],...]>>>forvalueinchain(*matrix):...print(value**2)...819641625436169

In this example, you usechain() to iterate over the rows of the matrix. To feed the rows intochain(), you use the unpacking operator (*). Inside the loop, you calculate and print the square of each value.

Usingchain(), like in this example, essentiallyflattens the matrix into a single iterable, helping you avoid anested loop, which can be difficult to read and understand in some contexts.

Repeating Actions a Predefined Number of Times

Iteration is all about repeating some fragment of code multiple times. As you’ve learned so far,for loops are designed to repeat a given set of actions on the items of an iterable. However, you can also use this type of loop to quickly iterate a specific number of times. This is useful when you need to repeat a bunch of statements, but they don’t operate on the items of an iterable.

Here’s a fun example aboutPenny andSheldon to illustrate this:

Python
>>>for_inrange(3):...print("Knock, knock, knock")...print("Penny!")...Knock, knock, knockPenny!Knock, knock, knockPenny!Knock, knock, knockPenny!

This loop runs three times and repeats a series of statements that don’t operate on any iterable. Note that the loop variable is a singleunderscore character in this example. This variable name communicates that you don’t need to use the loop variable inside the loop. It’s a throwaway variable.

With this looping construct that takes advantage ofrange(), you have full control over the number of times your code runs.

Iterating Over Reversed and Sorted Iterables

Iterating over the items of an iterable in reverse or sorted order is also a common requirement in programming. To achieve this, you can combine afor loop with the built-inreversed() orsorted() function, respectively.

For example, say that you’re working on a text editor and want to implement a basicUndo option. You can implement it with thereversed() function and a loop like the following:

Python
>>>actions=["Type text","Select text","Cut text","Paste text"]>>>foractioninreversed(actions):...print(f"Undo:{action}")...Undo: Paste textUndo: Cut textUndo: Select textUndo: Type text

In this example, you have a list of hypothetical user actions in a text editor. The actions are stored in a list from oldest to newest. To implement theUndo operation, you need to reverse the actions, which you do withreversed().

To iterate in sorted order, say that you have a dictionary that maps student names to their corresponding average grades. You need to create a quick report and want to sort the data from highest to lowest grades. For this, you can do something like the following:

Python
>>>students={..."Alice":89.5,..."Bob":76.0,..."Charlie":92.3,..."Diana":84.7,..."Ethan":88.9,..."Fiona":95.6,..."George":73.4,..."Hannah":81.2,...}>>>sorted_students=sorted(...students.items(),key=lambdaitem:item[1],reverse=True...)>>>forname,gradeinsorted_students:...print(f"{name}'s average grade:{grade:->{20-len(name)}.1f}")...Fiona's average grade: -----------95.6Charlie's average grade: ---------92.3Alice's average grade: -----------89.5Ethan's average grade: -----------88.9Diana's average grade: -----------84.7Hannah's average grade: ----------81.2Bob's average grade: -------------76.0George's average grade: ----------73.4

Thesorted() function returns a list of sorted values. In this example, you sort the dictionary by its values in ascending order. To do this, you use alambda function that takes a two-value tuple as an argument and returns the second item, which has an index of1. You also set thereverse argument toTrue so that the grades are ordered in descending order.

Thefor loop iterates over the sorted data and generates a nicely formatted report using anf-string with a customformat specifier.

Understanding Common Pitfalls infor Loops

When working withfor loops in your Python code, you may encounter some issues related to incorrect ways to use this tool. Some of the most common bad practices and incorrect assumptions include:

  • Modifying the loop collection or iterable during iteration
  • Changing the loop variable to affect the underlying collection
  • Ignoring possible exceptions that may occur

In the following sections, you’ll explore these pitfalls and how to avoid them in yourfor loops.

Modifying the Loop Collection

Python hasmutable collections, such as lists and dictionaries, that you can modify in place. You may want to change a list while looping over it. In this situation, you need to distinguish betweensafe andunsafe changes.

For example, say that you have a list of names and want to convert them into uppercase. You may think of doing something like the following:

Python
>>>names=["Alice","Bob","John","Jane"]>>>forindex,nameinenumerate(names):...names[index]=name.upper()...>>>names['ALICE', 'BOB', 'JOHN', 'JANE']

In this example, you only change the existing items in the list without adding or removing any. This operation is safe. However, modifying a mutable iterable like a list while iterating over it always raises a warning.

Issues may appear when you add or remove items from a list while iterating over it. To understand why this is best avoided, say that you want to remove all the even numbers from a list. You might write the following code:

Python
>>>numbers=[2,4,6,8]>>>fornumberinnumbers:...ifnumber%2==0:...numbers.remove(number)...>>>numbers[4, 8]

After running the loop, some even numbers remain, even though you expected the list to be empty.

On the first iteration,2 is removed, and the list shifts left, becoming[4, 6, 8]. The loop then jumps to the next item, skipping4 and processing6 instead. Then6 is removed, and the list shifts again, becoming[4, 8]. The iteration ends before reaching8.

When you need to resize a list during iteration like in the example above, it’s recommended to create a copy of the list:

Python
>>>numbers=[2,4,6,8]>>>fornumberinnumbers[:]:...ifnumber%2==0:...numbers.remove(number)...>>>numbers[]

The slicing operator ([:]) with no indices creates a copy of the original list for iteration purposes. The loop traverses the copy while removing values from the original list.

In some cases, creating a copy of the input list isn’t enough. Say that on top of removing even numbers, you want to calculate the square of odd numbers. You might modify the previous loop as shown in the following code:

Python
>>>numbers=[2,1,4,6,5,8]>>>forindex,numberinenumerate(numbers[:]):...ifnumber%2==0:...numbers.remove(number)...else:...numbers[index]=number**2...Traceback (most recent call last):...ValueError:list.remove(x): x not in list

This time, you useenumerate() to generate index-item pairs. Then, you think of using the index to update the value of a given item. However, the code fails with aValueError exception. Creating a copy of the input list isn’t enough in this case. You’d have to make a separate list to store the result:

Python
>>>numbers=[2,1,4,6,5,8]>>>processed_numbers=[]>>>fornumberinnumbers:...ifnumber%2!=0:...processed_numbers.append(number**2)...>>>processed_numbers[1, 25]

In this new loop implementation, you’re using a new list to store the result. Because of this, you don’t have to remove items anymore. You add the square values to the end of the new list using the.append() method.

Python doesn’t allow you to add or remove items from a dictionary while you’re iterating through it:

Python
>>>values={"one":1,"two":2,"three":3,"four":4}>>>forvalueinvalues:...values["five"]=5# Attempt to add an item...Traceback (most recent call last):...RuntimeError:dictionary changed size during iteration>>>forvalueinvalues:...delvalues[value]# Attempt to remove an item...Traceback (most recent call last):...RuntimeError:dictionary changed size during iteration

If you try to expand or shrink a dictionary during iteration, you get aRuntimeError exception. Again, you can work around this by creating a copy of the dictionary using the.copy() method or by building a new dictionary with the resulting data.

Changing the Loop Variable

Changing the loop variable in the loop body doesn’t have an effect on the original data:

Python
>>>names=["Alice","Bob","John","Jane"]>>>fornameinnames:...name=name.upper()...print(name)...ALICEBOBJOHNJANE>>>names['Alice', 'Bob', 'John', 'Jane']

In this example, the highlighted line changes the loop variable,name. This change doesn’t affect the original data in your list of names. The loop variable is just a temporary reference to the current item in the iterable, and reassigning it doesn’t affect the loop iterable.

Ignoring Possible Exceptions

If an exception occurs in a loop body and isn’t handled, the loop will terminate prematurely, skipping subsequent iterations. This result can generate unexpected issues, especially when you rely on the loop to process data, performlogging, or run cleanup actions in each iteration.

As an example, say that you want to process some text files in a loop:

Python
>>>files=["file1.txt","file2.txt","file3.txt"]>>>forfileinfiles:...withopen(file,"r")asf:...print(f"Contents of{file}:")...print(f.read())...Traceback (most recent call last):...FileNotFoundError:[Errno 2] No such file or directory: 'file1.txt'

In this example, none of the files exist in your working directory. The loop tries to process the first file and fails with aFileNotFoundError exception. Because the exception wasn’t handled properly, the loop terminates in the first iteration, skipping the rest of the files in the list.

To avoid this behavior, you need to catch and handle the exception:

Python
>>>files=["file1.txt","file2.txt","file3.txt"]>>>forfileinfiles:...try:...withopen(file,"r")asf:...print(f"Contents of{file}:")...print(f.read())...exceptFileNotFoundError:...print(f"Error:{file} not found. Skipping.")...Error: file1.txt not found. Skipping.Error: file2.txt not found. Skipping.Error: file3.txt not found. Skipping.

In this new implementation, the loop catches anyFileNotFoundError exception and prints an error message to the screen. The loop runs entirely without abrupt interruptions.

Usingfor Loops vs Comprehensions

When you usefor loops to transform data and build new collections, it may be possible to replace the loop with a comprehension. For example, consider the loop below:

Python
>>>cubes=[]>>>fornumberinrange(10):...cubes.append(number**3)...>>>cubes[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

In this example, you first define an empty list calledcubes. Then, you use a loop to iterate over a range of integer numbers and populate the list with cube values.

You can quickly replace the above loop with a list comprehension like the following:

Python
>>>cubes=[number**3fornumberinrange(10)]>>>cubes[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

The comprehension iterates over the range of numbers and builds the list of cubes in a single line of code.

Usingasync for Loops for Asynchronous Iteration

Theasync for statement allows you to create loops that iterate overasynchronous iterables. This type of loop works pretty much the same as regularfor loops, but the loop collection must be anasynchronous iterator or iterable.

Note: To learn more aboutasynchronous iteration, check out theAsynchronous Iterators and Iterables in Python tutorial.

The example below shows anAsyncRange class that generates ranges of integer values asynchronously. You can use this iterable in anasync for loop:

Pythonasync_range.py
importasyncioclassAsyncRange:def__init__(self,start,end):self.data=range(start,end)asyncdef__aiter__(self):forindexinself.data:awaitasyncio.sleep(0.5)yieldindexasyncdefmain():asyncforindexinAsyncRange(0,5):print(index)asyncio.run(main())

In this code, the loop in the highlighted line iterates over integer indices from0 to5 in an asynchronous manner.

When yourun this script, you get the following output:

Shell
$pythonasync_range.py01234

In this output, each number is obtained after waiting half a second, which is consistent with the asynchronous iteration.

Conclusion

You’ve learned how to use Pythonfor loops to iterate over various data collections, including lists, tuples, strings, dictionaries, and sets. You’ve explored advanced loop features like thebreak andcontinue statements, theelse clause, and nested loops. Additionally, you learned about Pythonic looping techniques, common pitfalls, and the use ofasync for loops for asynchronous iteration.

Understandingfor loops is essential for Python developers, as they offer an efficient way to manage repetitive tasks and process data. Masteringfor loops helps you write code that is more Pythonic, performant, and easier to maintain.

In this tutorial, you’ve learned how to:

  • Iterate over different Python collections usingfor loops
  • Useadvanced features likebreak,continue, andelse in loops
  • ApplyPythonic techniques for cleaner and more efficient loops
  • Work aroundcommon pitfalls when working with loops
  • Useasync for loops for asynchronous data processing

With these skills, you can now write more efficient and readable Python code that leverages the power offor loops to handle a wide range of data processing tasks.

Get Your Code:Click here to download the free sample code that shows you how to use for loops in Python.

Frequently Asked Questions

Now that you have some experience with Pythonfor loops, you can use the questions and answers below to check your understanding and recap what you’ve learned.

These FAQs are related to the most important concepts you’ve covered in this tutorial. Click theShow/Hide toggle beside each question to reveal the answer.

You use afor loop to iterate over a list by specifying the loop variable and the list. For example,for item in a_list: allows you to process each item ina_list.

An iterable is an object capable of returning its members one at a time, while an iterator is an object that represents a stream of data, returning the next item with a.__next__() special method.

You can iterate over both keys and values in a dictionary using the.items() method in afor loop, likefor key, value in a_dict.items():.

If you modify a list while iterating over it, you may encounter unexpected behavior, such as skipping elements or runtime errors. It’s recommended to iterate over a copy of the list instead. In some cases, it’s necessary to create a new list to keep the result.

To handle exceptions in afor loop, wrap the code that might raise an exception in atry block. Then use anexcept block to catch and handle the exception, allowing the loop to continue.

Take the Quiz: Test your knowledge with our interactive “Python for Loops: The Pythonic Way” quiz. You’ll receive a score upon completion to help you track your learning progress:


Python for Loops: The Pythonic Way

Interactive Quiz

Python for Loops: The Pythonic Way

In this quiz, you'll test your understanding of Python's for loop. You'll revisit how to iterate over items in a data collection, how to use range() for a predefined number of iterations, and how to use enumerate() for index-based iteration.

Recommended Course

For Loops in Python (Definite Iteration)(16m)

🐍 Python Tricks 💌

Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Python Tricks Dictionary Merge

AboutLeodanis Pozo Ramos

Leodanis is a self-taught Python developer, educator, and technical writer with over 10 years of experience.

» More about Leodanis

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

MasterReal-World Python Skills With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

MasterReal-World Python Skills
With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.


Looking for a real-time conversation? Visit theReal Python Community Chat or join the next“Office Hours” Live Q&A Session. Happy Pythoning!

Keep Learning

Related Topics:intermediatebest-practicespython

Related Learning Paths:

Related Courses:

Related Tutorials:

Keep reading Real Python by creating a free account or signing in:

Already have an account?Sign-In

Almost there! Complete this form and click the button below to gain instant access:

Python for Loops: The Pythonic Way

Python for Loops: The Pythonic Way (Sample Code)

🔒 No spam. We take your privacy seriously.


[8]ページ先頭

©2009-2026 Movatter.jp