Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Storing Filtered Data in a Tuple

Give Feedback

In this lesson, you’ll see how you can ensure that your filtered down data is safely stored in a tuple, which is an immutable data structure. You’ll use the tuple() function and wrap it around filter(). Now you have all of the elements generated by this iterator stored inside a tuple.

Then you’ll walk through another quick example to review how you can use filter() to see which items in your data set meet specific criteria. You’ll be using lambda expressions again.

Comments & Discussion

Choong Han on March 31, 2020

Is it possible to set the condition as a range in the lambda function like the year of when their are born is larger than 1900?

Dan Bader RP Team on March 31, 2020

Yes absolutely, you could do something like this:

lambda x: 1900 < x.born < 1930

Or:

lambda x: x in range(1900, 1931)

Mike Leo on April 9, 2020

How did it go from a generator to a tuple?

I would have expected you to have to call the generator until the end to “fill up” the tuple?

Dan Bader RP Team on April 9, 2020

@Mike: When you pass a generator to a collection constructor like list() or tuple(), the collection automatically “consumes” the generator and fills itself with the elements yielded by the generator.

Become a Member to join the conversation.