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.
Hint: You can set the default subtitles language 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.

00:00 What I want to do now is actually turn this into what I promised you earlier. I want to turn this into another list or, I guess, a tuple—to make it immutable—of these filtered-down scientists.

00:12 What I can do here is we could say—okay. Basically, what I’m going to do here is I’m going to bring back this filter() construction—or, this filter() function call,

00:26 and I’m going to call tuple() on it. That will take all of the elements generated by this iterator and it will put them inside a tuple that we can then refer to. All right?

00:38 So, if I wanted to make this prettier, I could do this,

00:44 and pprint(fs), and then you would see that it’s filtered down to only include Nobel

00:52 Prize scientists. The filter() function is pretty cool because it allows you to apply a function over a whole list of things, or an iterable of things, and then every item in this list, or in this iterable, gets passed to the function—in this case, it would be xand then we can make a decision and return True or False. And if we return True, this item will be included in the resulting list, in a filtered list, and if we return False it will not be included.

01:22 Maybe to show you how that works again—so, I’m going to make this slightly crazy expression here, all right? So here, I just want to type out a skeleton here—scientists.

01:41 Okay, so what I’m doing here is I’ve defined this expression here that would actually convert it to a tuple and then print it out so it looks nice and clean, and I didn’t yet add the filter() function, which we’re going to do now. In this case, you know, I could just always return True.

01:57 So, if the filter() function would always return True, then we are just going to return the whole list, right? And you can filter on anything you want, right? I could say, okay, if x.field == 'physics', we’re only going to get people that worked in physics.

02:17 And if x.field == 'physics' and x.nobellet’s actually use the short version this time, x.nobelthen I get a tuple of length 1, because that’s the only person. Marie Curry, here, is the only person that this filter() expression matched on.

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.

nwobodoe71 on Aug. 22, 2020

why use pprint instead of print?

Dan Bader RP Team on Aug. 24, 2020

pprint is a “pretty printer” module included with the Python standard library (pprint == “pretty print”) that outputs nicer-looking representations of the printed data structures that can be easier to read.

For example, pprint spreads out the contents of a collection or sequence over multiple lines instead of putting them all on a single line (like the regular print function).

The pprint module quite handy, you should check it out! More info here in the official docs: docs.python.org/3/library/pprint.html

Siddhanth Kasat on Sept. 16, 2020

I am not sure if it is a valid comment, but what IDE are you using?

Bartosz Zaczyński RP Team on Sept. 16, 2020

I don’t know about a particular IDE that Dan might be fond of, but here in the video, it looks like he’s using bpython wrapper for the interactive Python shell.

Siddhanth Kasat on Sept. 17, 2020

Thank you, I love the way it just pops documentation up every time you use some function. I do some teaching myself and I think it is a great tool for beginners.

May the Force be with you!

Bartosz Zaczyński RP Team on Sept. 17, 2020

Exactly! But, not only is it great for beginners. Anyone can use bpython to explore the API of a new library, for example. There’s much more you can do with it: A whirlwind tour of bpython

Become a Member to join the conversation.