Movatterモバイル変換


[0]ホーム

URL:


— FREE Email Series —

🐍 Python Tricks 💌

Python Tricks Dictionary Merge

🔒 No spam. Unsubscribe any time.

Browse TopicsGuided Learning Paths
Basics Intermediate Advanced
apibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnumpyprojectspythontestingtoolsweb-devweb-scraping

Table of Contents

Recommended Video Course
Python Generators 101

How to Use Generators and Yield in Python

How to Use Generators and yield in Python

byKyle Stratisintermediatepython

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Python Generators 101

Have you ever had to work with a dataset so large that it overwhelmed your machine’s memory? Or maybe you have a complex function that needs to maintain an internal state every time it’s called, but the function is too small to justify creating its own class. In these cases and more, generators and the Python yield statement are here to help.

By the end of this article, you’ll know:

  • Whatgenerators are and how to use them
  • How to creategenerator functions and expressions
  • How thePython yield statement works
  • How to usemultiple Python yield statements in a generator function
  • How to useadvanced generator methods
  • How tobuild data pipelines with multiple generators

If you’re abeginner or intermediate Pythonista and you’re interested in learning how to work with large datasets in a more Pythonic fashion, then this is the tutorial for you.

You can get a copy of the dataset used in this tutorial by clicking the link below:

Download Dataset:Click here to download the dataset you’ll use in this tutorial to learn about generators and yield in Python.

Take the Quiz: Test your knowledge with our interactive “How to Use Generators and yield in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


How to Use Generators and Yield in Python

Interactive Quiz

How to Use Generators and yield in Python

In this quiz, you'll test your understanding of Python generators and the yield statement. With this knowledge, you'll be able to work with large datasets in a more Pythonic fashion, create generator functions and expressions, and build data pipelines.

Using Generators

Introduced withPEP 255,generator functions are a special kind of function that return alazy iterator. These are objects that you can loop over like alist. However, unlike lists, lazy iterators do not store their contents in memory. For an overview of iterators in Python, take a look atPython “for” Loops (Definite Iteration).

Now that you have a rough idea of what a generator does, you might wonder what they look like in action. Let’s take a look at two examples. In the first, you’ll see how generators work from a bird’s eye view. Then, you’ll zoom in and examine each example more thoroughly.

Example 1: Reading Large Files

A common use case of generators is towork with data streams or large files, likeCSV files. These text files separate data into columns by using commas. This format is a common way to share data. Now, what if you want to count the number of rows in a CSV file? The code block below shows one way of counting those rows:

Python
csv_gen=csv_reader("some_csv.txt")row_count=0forrowincsv_gen:row_count+=1print(f"Row count is{row_count}")

Looking at this example, you might expectcsv_gen to be a list. To populate this list,csv_reader() opens a file and loads its contents intocsv_gen. Then, the program iterates over the list and incrementsrow_count for each row.

This is a reasonable explanation, but would this design still work if the file is very large? What if the file is larger than the memory you have available? To answer this question, let’s assume thatcsv_reader() just opens the file and reads it into an array:

Python
defcsv_reader(file_name):file=open(file_name)result=file.read().split("\n")returnresult

This function opens a given file and usesfile.read() along with.split() to add each line as a separate element to a list. If you were to use this version ofcsv_reader() in the row counting code block you saw further up, then you’d get the following output:

Python
Traceback (most recent call last):  File"ex1_naive.py", line22, in<module>main()  File"ex1_naive.py", line13, inmaincsv_gen=csv_reader("file.txt")  File"ex1_naive.py", line6, incsv_readerresult=file.read().split("\n")MemoryError

In this case,open() returns a generator object that you can lazily iterate through line by line. However,file.read().split() loads everything into memory at once, causing theMemoryError.

Before that happens, you’ll probably notice your computer slow to a crawl. You might even need to kill the program with aKeyboardInterrupt. So, how can you handle these huge data files? Take a look at a new definition ofcsv_reader():

Python
defcsv_reader(file_name):forrowinopen(file_name,"r"):yieldrow

In this version, you open the file, iterate through it, and yield a row. This code should produce the following output, with no memory errors:

Shell
Row count is 64186394

What’s happening here? Well, you’ve essentially turnedcsv_reader() into a generator function. This version opens a file, loops through each line, and yields each row, instead of returning it.

You can also define agenerator expression (also called agenerator comprehension), which has a very similar syntax tolist comprehensions. In this way, you can use the generator without calling a function:

Python
csv_gen=(rowforrowinopen(file_name))

This is a more succinct way to create the listcsv_gen. You’ll learn more about the Python yield statement soon. For now, just remember this key difference:

  • Usingyield will result in a generator object.
  • Usingreturn will result in the first line of the fileonly.

Example 2: Generating an Infinite Sequence

Let’s switch gears and look at infinite sequence generation. In Python, to get a finite sequence, you callrange() and evaluate it in a list context:

Python
>>>a=range(5)>>>list(a)[0, 1, 2, 3, 4]

Generating aninfinite sequence, however, will require the use of a generator, since your computer memory is finite:

Python
definfinite_sequence():num=0whileTrue:yieldnumnum+=1

This code block is short and sweet. First, you initialize thevariablenum and start an infinite loop. Then, you immediatelyyield num so that you can capture the initial state. This mimics the action ofrange().

Afteryield, you incrementnum by 1. If you try this with afor loop, then you’ll see that it really does seem infinite:

Python
>>>foriininfinite_sequence():...print(i,end=" ")...0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2930 31 32 33 34 35 36 37 38 39 40 41 42[...]6157818 6157819 6157820 6157821 6157822 6157823 6157824 6157825 6157826 61578276157828 6157829 6157830 6157831 6157832 6157833 6157834 6157835 6157836 61578376157838 6157839 6157840 6157841 6157842Traceback (most recent call last):  File"<stdin>", line2, in<module>KeyboardInterrupt

The program will continue to execute until you stop it manually.

Instead of using afor loop, you can also callnext() on the generator object directly. This is especially useful for testing a generator in the console:

Python
>>>gen=infinite_sequence()>>>next(gen)0>>>next(gen)1>>>next(gen)2>>>next(gen)3

Here, you have a generator calledgen, which you manually iterate over by repeatedly callingnext(). This works as a great sanity check to make sure your generators are producing the output you expect.

Note: When you usenext(), Python calls.__next__() on the function you pass in as a parameter. There are some special effects that this parameterization allows, but it goes beyond the scope of this article. Experiment with changing the parameter you pass tonext() and see what happens!

Example 3: Detecting Palindromes

You can use infinite sequences in many ways, but one practical use for them is in building palindrome detectors. Apalindrome detector will locate all sequences of letters ornumbers that are palindromes. These are words or numbers that are read the same forward and backward, like 121. First, define your numeric palindrome detector:

Python
defis_palindrome(num):# Skip single-digit inputsifnum//10==0:returnFalsetemp=numreversed_num=0whiletemp!=0:reversed_num=(reversed_num*10)+(temp%10)temp=temp//10ifnum==reversed_num:returnnumelse:returnFalse

Don’t worry too much about understanding the underlying math in this code. Just note that the function takes an input number, reverses it, and checks to see if the reversed number is the same as the original. Now you can use your infinite sequence generator to get a running list of all numeric palindromes:

Python
>>>foriininfinite_sequence():...pal=is_palindrome(i)...ifpal:...print(i)...112233[...]997999989999999100001101101102201Traceback (most recent call last):  File"<stdin>", line2, in<module>  File"<stdin>", line5, inis_palindromeKeyboardInterrupt

In this case, the only numbers that areprinted to the console are those that are the same forward or backward.

Note: In practice, you’re unlikely to write your own infinite sequence generator. Theitertools module provides a very efficient infinite sequence generator withitertools.count().

Now that you’ve seen a simple use case for an infinite sequence generator, let’s dive deeper into how generators work.

Understanding Generators

So far, you’ve learned about the two primary ways of creating generators: by using generator functions and generator expressions. You might even have an intuitive understanding of how generators work. Let’s take a moment to make that knowledge a little more explicit.

Generator functions look and act just like regular functions, but with one defining characteristic. Generator functions use the Pythonyield keyword instead ofreturn. Recall the generator function you wrote earlier:

Python
definfinite_sequence():num=0whileTrue:yieldnumnum+=1

This looks like a typicalfunction definition, except for the Python yield statement and the code that follows it.yield indicates where a value is sent back to the caller, but unlikereturn, you don’t exit the function afterward.

Instead, thestate of the function is remembered. That way, whennext() is called on a generator object (either explicitly or implicitly within afor loop), the previously yielded variablenum is incremented, and then yielded again. Since generator functions look like other functions and act very similarly to them, you can assume that generator expressions are very similar to other comprehensions available in Python.

Note: Are you rusty on Python’s list, set, and dictionary comprehensions? You can check outUsing List Comprehensions Effectively.

Building Generators With Generator Expressions

Like list comprehensions, generator expressions allow you to quickly create a generator object in just a few lines of code. They’re also useful in the same cases where list comprehensions are used, with an added benefit: you can create them without building and holding the entire object in memory before iteration. In other words, you’ll have no memory penalty when you use generator expressions. Take this example of squaring some numbers:

Python
>>>nums_squared_lc=[num**2fornuminrange(5)]>>>nums_squared_gc=(num**2fornuminrange(5))

Bothnums_squared_lc andnums_squared_gc look basically the same, but there’s one key difference. Can you spot it? Take a look at what happens when you inspect each of these objects:

Python
>>>nums_squared_lc[0, 1, 4, 9, 16]>>>nums_squared_gc<generator object <genexpr> at 0x107fbbc78>

The first object used brackets to build a list, while the second created a generator expression by using parentheses. The output confirms that you’ve created a generator object and that it is distinct from a list.

Profiling Generator Performance

You learned earlier that generators are a great way to optimize memory. While an infinite sequence generator is an extreme example of this optimization, let’s amp up the number squaring examples you just saw and inspect the size of the resulting objects. You can do this with a call tosys.getsizeof():

Python
>>>importsys>>>nums_squared_lc=[i**2foriinrange(10000)]>>>sys.getsizeof(nums_squared_lc)87624>>>nums_squared_gc=(i**2foriinrange(10000))>>>print(sys.getsizeof(nums_squared_gc))120

In this case, the list you get from the list comprehension is 87,624 bytes, while the generator object is only 120. This means that the list is over 700 times larger than the generator object!

There is one thing to keep in mind, though. If the list is smaller than the running machine’s available memory, then list comprehensions can befaster to evaluate than the equivalent generator expression. To explore this, let’s sum across the results from the two comprehensions above. You can generate a readout withcProfile.run():

Python
>>>importcProfile>>>cProfile.run('sum([i * 2 for i in range(10000)])')         5 function calls in 0.001 seconds   Ordered by: standard name   ncalls  tottime  percall  cumtime  percall filename:lineno(function)        1    0.001    0.001    0.001    0.001 <string>:1(<listcomp>)        1    0.000    0.000    0.001    0.001 <string>:1(<module>)        1    0.000    0.000    0.001    0.001 {built-in method builtins.exec}        1    0.000    0.000    0.000    0.000 {built-in method builtins.sum}        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}>>>cProfile.run('sum((i * 2 for i in range(10000)))')         10005 function calls in 0.003 seconds   Ordered by: standard name   ncalls  tottime  percall  cumtime  percall filename:lineno(function)    10001    0.002    0.000    0.002    0.000 <string>:1(<genexpr>)        1    0.000    0.000    0.003    0.003 <string>:1(<module>)        1    0.000    0.000    0.003    0.003 {built-in method builtins.exec}        1    0.001    0.001    0.003    0.003 {built-in method builtins.sum}        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Here, you can see that summing across all values in the list comprehension took about a third of the time as summing across the generator. If speed is an issue and memory isn’t, then a list comprehension is likely a better tool for the job.

Note: These measurements aren’t only valid for objects made with generator expressions. They’re also the same for objects made from the analogous generator function since the resulting generators are equivalent.

Remember, list comprehensions return full lists, while generator expressions return generators. Generators work the same whether they’re built from a function or an expression. Using an expression just allows you to define simple generators in a single line, with an assumedyield at the end of each inner iteration.

The Python yield statement is certainly the linchpin on which all of the functionality of generators rests, so let’s dive into howyield works in Python.

Understanding the Python Yield Statement

On the whole,yield is a fairly simple statement. Its primary job is to control the flow of a generator function in a way that’s similar toreturn statements. As briefly mentioned above, though, the Python yield statement has a few tricks up its sleeve.

When you call a generator function or use a generator expression, you return a specialiterator called a generator. You can assign this generator to a variable in order to use it. When you call special methods on the generator, such asnext(), the code within the function is executed up toyield.

When the Python yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (In contrast,return stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator, the instruction pointer, the internal stack, and any exception handling.

This allows you to resume function execution whenever you call one of the generator’s methods. In this way, all function evaluation picks back up right afteryield. You can see this in action by using multiple Python yield statements:

Python
>>>defmulti_yield():...yield_str="This will print the first string"...yieldyield_str...yield_str="This will print the second string"...yieldyield_str...>>>multi_obj=multi_yield()>>>print(next(multi_obj))This will print the first string>>>print(next(multi_obj))This will print the second string>>>print(next(multi_obj))Traceback (most recent call last):  File"<stdin>", line1, in<module>StopIteration

Take a closer look at that last call tonext(). You can see that execution has blown up with atraceback. This is because generators, like all iterators, can be exhausted. Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and thefor loop will exit. If you usednext(), then instead you’ll get an explicitStopIteration exception.

Note:StopIteration is a natural exception that’s raised to signal the end of an iterator.for loops, for example, are built aroundStopIteration. You can even implement your ownfor loop by using awhile loop:

Python
>>>letters=["a","b","c","y"]>>>it=iter(letters)>>>whileTrue:...try:...letter=next(it)...exceptStopIteration:...break...print(letter)...abcy

You can read more aboutStopIteration in the Python documentation onexceptions. For more on iteration in general, check outPython “for” Loops (Definite Iteration) andPython “while” Loops (Indefinite Iteration).

yield can be used in many ways to control your generator’s execution flow. The use of multiple Python yield statements can be leveraged as far as your creativity allows.

Using Advanced Generator Methods

You’ve seen the most common uses and constructions of generators, but there are a few more tricks to cover. In addition toyield, generator objects can make use of the following methods:

  • .send()
  • .throw()
  • .close()

How to Use.send()

For this next section, you’re going to build a program that makes use of all three methods. This program will print numeric palindromes like before, but with a few tweaks. Upon encountering a palindrome, your new program will add a digit and start a search for the next one from there. You’ll also handle exceptions with.throw() and stop the generator after a given amount of digits with.close(). First, let’s recall the code for your palindrome detector:

Python
defis_palindrome(num):# Skip single-digit inputsifnum//10==0:returnFalsetemp=numreversed_num=0whiletemp!=0:reversed_num=(reversed_num*10)+(temp%10)temp=temp//10ifnum==reversed_num:returnTrueelse:returnFalse

This is the same code you saw earlier, except that now the program returns strictlyTrue orFalse. You’ll also need to modify your original infinite sequence generator, like so:

Python
 1definfinite_palindromes(): 2num=0 3whileTrue: 4ifis_palindrome(num): 5i=(yieldnum) 6ifiisnotNone: 7num=i 8num+=1

There are a lot of changes here! The first one you’ll see is in line 5, wherei = (yield num). Though you learned earlier thatyield is a statement, that isn’t quite the whole story.

As of Python 2.5 (the same release that introduced the methods you are learning about now),yield is anexpression, rather than a statement. Of course, you can still use it as a statement. But now, you can also use it as you see in the code block above, wherei takes the value that is yielded. This allows you to manipulate the yielded value. More importantly, it allows you to.send() a value back to the generator. When execution picks up afteryield,i will take the value that is sent.

You’ll also checkif i is not None, which could happen ifnext() is called on the generator object. (This can also happen when you iterate with afor loop.) Ifi has a value, then you updatenum with the new value. But regardless of whether or noti holds a value, you’ll then incrementnum and start the loop again.

Now, take a look at the main function code, which sends the lowest number with another digit back to the generator. For example, if the palindrome is 121, then it will.send() 1000:

Python
pal_gen=infinite_palindromes()foriinpal_gen:digits=len(str(i))pal_gen.send(10**(digits))

With this code, you create the generator object and iterate through it. The program only yields a value once a palindrome is found. It useslen() to determine the number of digits in that palindrome. Then, it sends10 ** digits to the generator. This brings execution back into the generator logic and assigns10 ** digits toi. Sincei now has a value, the program updatesnum, increments, and checks for palindromes again.

Once your code finds and yields another palindrome, you’ll iterate via thefor loop. This is the same as iterating withnext(). The generator also picks up at line 5 withi = (yield num). However, nowi isNone, because you didn’t explicitly send a value.

What you’ve created here is acoroutine, or a generator function into which you can pass data. These are useful for constructing data pipelines, but as you’ll see soon, they aren’t necessary for building them. (If you’re looking to dive deeper, thenthis course on coroutines and concurrency is one of the most comprehensive treatments available.)

Now that you’ve learned about.send(), let’s take a look at.throw().

How to Use.throw()

.throw() allows you to throw exceptions with the generator. In the below example, you raise the exception in line 6. This code will throw aValueError oncedigits reaches 5:

Python
 1pal_gen=infinite_palindromes() 2foriinpal_gen: 3print(i) 4digits=len(str(i)) 5ifdigits==5: 6pal_gen.throw(ValueError("We don't like large palindromes")) 7pal_gen.send(10**(digits))

This is the same as the previous code, but now you’ll check ifdigits is equal to 5. If so, then you’ll.throw() aValueError. To confirm that this works as expected, take a look at the code’s output:

Python
11111111110101Traceback (most recent call last):  File"advanced_gen.py", line47, in<module>main()  File"advanced_gen.py", line41, inmainpal_gen.throw(ValueError("We don't like large palindromes"))  File"advanced_gen.py", line26, ininfinite_palindromesi=(yieldnum)ValueError:We don't like large palindromes

.throw() is useful in any areas where you might need to catch anexception. In this example, you used.throw() to control when you stopped iterating through the generator. You can do this more elegantly with.close().

How to Use.close()

As its name implies,.close() allows you to stop a generator. This can be especially handy when controlling an infinite sequence generator. Let’s update the code above by changing.throw() to.close() to stop the iteration:

Python
 1pal_gen=infinite_palindromes() 2foriinpal_gen: 3print(i) 4digits=len(str(i)) 5ifdigits==5: 6pal_gen.close() 7pal_gen.send(10**(digits))

Instead of calling.throw(), you use.close() in line 6. The advantage of using.close() is that it raisesStopIteration, an exception used to signal the end of a finite iterator:

Python
11111111110101Traceback (most recent call last):  File"advanced_gen.py", line46, in<module>main()  File"advanced_gen.py", line42, inmainpal_gen.send(10**(digits))StopIteration

Now that you’ve learned more about the special methods that come with generators, let’s talk about using generators to build data pipelines.

Creating Data Pipelines With Generators

Data pipelines allow you to string together code to process large datasets or streams of data without maxing out your machine’s memory. Imagine that you have a large CSV file with the following first lines:

CSV
permalink,company,numEmps,category,city,state,fundedDate,raisedAmt,raisedCurrency,rounddigg,Digg,60,web,San Francisco,CA,1-Dec-06,8500000,USD,bdigg,Digg,60,web,San Francisco,CA,1-Oct-05,2800000,USD,afacebook,Facebook,450,web,Palo Alto,CA,1-Sep-04,500000,USD,angelfacebook,Facebook,450,web,Palo Alto,CA,1-May-05,12700000,USD,aphotobucket,Photobucket,60,web,Palo Alto,CA,1-Mar-05,3000000,USD,a

This example is pulled from the TechCrunch Continental USA set, which describes funding rounds and dollar amounts for various startups based in the USA. Click the link below to download the dataset:

Download Dataset:Click here to download the dataset you’ll use in this tutorial to learn about generators and yield in Python.

It’s time to do some processing in Python! To demonstrate how to build pipelines with generators, you’re going to analyze this file to get the total and average of all series A rounds in the dataset.

Let’s think of a strategy:

  1. Read every line of the file.
  2. Split each line into a list of values.
  3. Extract the column names.
  4. Use the column names and lists to create a dictionary.
  5. Filter out the rounds you aren’t interested in.
  6. Calculate the total and average values for the rounds you are interested in.

Normally, you can do this with a package likepandas, but you can also achieve this functionality with just a few generators. You’ll start by reading each line from the file with a generator expression:

Python
 1file_name="techcrunch.csv" 2lines=(lineforlineinopen(file_name))

Then, you’ll use another generator expression in concert with the previous one to split each line into a list:

Python
 3list_line=(s.rstrip().split(",")forsinlines)

Here, you created the generatorlist_line, which iterates through the first generatorlines. This is a common pattern to use when designing generator pipelines. Next, you’ll pull the column names out oftechcrunch.csv. Since the column names tend to make up the first line in a CSV file, you can grab that with a shortnext() call:

Python
 4cols=next(list_line)

This call tonext() advances the iterator over thelist_line generator one time. Put it all together, and your code should look something like this:

Python
 1file_name="techcrunch.csv" 2lines=(lineforlineinopen(file_name)) 3list_line=(s.rstrip().split(",")forsinlines) 4cols=next(list_line)

To sum this up, you first create a generator expressionlines to yield each line in a file. Next, you iterate through that generator within the definition ofanother generator expression calledlist_line, which turns each line into a list of values. Then, you advance the iteration oflist_line just once withnext() to get a list of the column names from your CSV file.

Note: Watch out for trailing newlines! This code takes advantage of.rstrip() in thelist_line generator expression to make sure there are no trailing newline characters, which can be present in CSV files.

To help you filter and perform operations on the data, you’ll create dictionaries where the keys are the column names from the CSV:

Python
 5company_dicts=(dict(zip(cols,data))fordatainlist_line)

This generator expression iterates through the lists produced bylist_line. Then, it useszip() anddict() to create the dictionary as specified above. Now, you’ll use afourth generator to filter the funding round you want and pullraisedAmt as well:

Python
 6funding=( 7int(company_dict["raisedAmt"]) 8forcompany_dictincompany_dicts 9ifcompany_dict["round"]=="a"10)

In this code snippet, your generator expression iterates through the results ofcompany_dicts and takes theraisedAmt for anycompany_dict where theround key is"a".

Remember, you aren’t iterating through all these at once in the generator expression. In fact, you aren’t iterating through anything until you actually use afor loop or a function that works oniterables, likesum(). In fact, callsum() now to iterate through the generators:

Python
11total_series_a=sum(funding)

Putting this all together, you’ll produce the following script:

Python
 1file_name="techcrunch.csv" 2lines=(lineforlineinopen(file_name)) 3list_line=(s.rstrip().split(",")forsinlines) 4cols=next(list_line) 5company_dicts=(dict(zip(cols,data))fordatainlist_line) 6funding=( 7int(company_dict["raisedAmt"]) 8forcompany_dictincompany_dicts 9ifcompany_dict["round"]=="a"10)11total_series_a=sum(funding)12print(f"Total series A fundraising: ${total_series_a}")

This script pulls together every generator you’ve built, and they all function as one big data pipeline. Here’s a line by line breakdown:

  • Line 2 reads in each line of the file.
  • Line 3 splits each line into values and puts the values into a list.
  • Line 4 usesnext() to store the column names in a list.
  • Line 5 creates dictionaries and unites them with azip() call:
    • The keys are the column namescols from line 4.
    • The values are the rows in list form, created in line 3.
  • Line 6 gets each company’s series A funding amounts. It also filters out any other raised amount.
  • Line 11 begins the iteration process by callingsum() to get the total amount of series A funding found in the CSV.

When you run this code ontechcrunch.csv, you should find a total of $4,380,015,000 raised in series A funding rounds.

Note: The methods for handling CSV files developed in this tutorial are important for understanding how to use generators and the Python yield statement. However, when you work with CSV files in Python, you should instead use thecsv module included in Python’s standard library. This module can parse comma-separated files more robustly and has optimized methods for handling them efficiently.

To dig even deeper, try figuring out the average amount raisedper company in a series A round. This is a bit trickier, so here are some hints:

  • Generators exhaust themselves after being iterated over fully.
  • You will still need thesum() function.

Good luck!

Conclusion

In this tutorial, you’ve learned aboutgenerator functions andgenerator expressions.

You now know:

  • How to use and write generator functions and generator expressions
  • How the all-importantPython yield statement enables generators
  • How to usemultiple Python yield statements in a generator function
  • How to use.send() to send data to a generator
  • How to use.throw() to raise generator exceptions
  • How to use.close() to stop a generator’s iteration
  • How tobuild a generator pipeline to efficiently process large CSV files

You can get the dataset you used in this tutorial at the link below:

Download Dataset:Click here to download the dataset you’ll use in this tutorial to learn about generators and yield in Python.

How have generators helped you in your work or projects? If you’re just learning about them, then how do you plan to use them in the future? Did you find a good solution to the data pipeline problem? Let us know in the comments below!

Take the Quiz: Test your knowledge with our interactive “How to Use Generators and yield in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


How to Use Generators and Yield in Python

Interactive Quiz

How to Use Generators and yield in Python

In this quiz, you'll test your understanding of Python generators and the yield statement. With this knowledge, you'll be able to work with large datasets in a more Pythonic fashion, create generator functions and expressions, and build data pipelines.

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Python Generators 101

🐍 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

AboutKyle Stratis

Kyle is a self-taught developer working as a senior data engineer at Vizit Labs. In the past, he has founded DanqEx (formerly Nasdanq: the original meme stock exchange) and Encryptid Gaming.

» More about Kyle

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:intermediatepython

Recommended Video Course:Python Generators 101

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 Data Science Artwork

Generators and Yield in Python (Dataset)

🔒 No spam. We take your privacy seriously.


[8]ページ先頭

©2009-2025 Movatter.jp