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.

Immutable Data Structures: Tuples

Give Feedback

In the previous lesson, you stored your immutable data structures (namedtuple) in an mutable one (list). Now, you’ll see how you can replace that list with a tuple, which is like a list but immutable:

import collections

Scientist = collections.namedtuple('Scientist', [
    'name',
    'field',
    'born',
    'nobel',
])

scientists = (
    Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),
    Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),
    Scientist(name='Marie Curie', field='math', born=1867, nobel=True),
    Scientist(name='Tu Youyou', field='physics', born=1930, nobel=True),
    Scientist(name='Ada Yonath', field='chemistry', born=1939, nobel=True),
    Scientist(name='Vera Rubin', field='chemistry', born=1928, nobel=False),
    Scientist(name='Sally Ride', field='physics', born=1951, nobel=False),
)

Now, you can access all of your data by index, but you’re no longer in danger of tampering with it. That’s exactly what you want to have when you’re taking a functional programming approach with a data set!

Comments & Discussion

Xavier on Nov. 12, 2019

Some mistakes in the field values which will result in different answers in Section 2 on the filter function. They should be:

scientists = (
    Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),
    Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),
    Scientist(name='Marie Curie', field='physics', born=1867, nobel=True),
    Scientist(name='Tu Youyou', field='chemistry', born=1930, nobel=True),
    Scientist(name='Ada Yonath', field='chemistry', born=1939, nobel=True),
    Scientist(name='Vera Rubin', field='astronomy', born=1928, nobel=False),
    Scientist(name='Sally Ride', field='physics', born=1951, nobel=False),
)

victorariasvanegas on March 26, 2020

Really, really good explained concept.

abrophy89 on April 8, 2020

How do you get your editor to default to showing you the help information within the square as shown in your video when you are in interactive prompt?

Varun Vaddiparty on May 10, 2020

@abrophy89 he is using an alternate REPL called bpython. Check it out.

M Schulze on May 24, 2020

I wonder about the naming convention with Scientist in upper case, is this based on Type Variables Names?

www.python.org/dev/peps/pep-0008/#type-variable-names

Dan Bader RP Team on May 24, 2020

I’m following the naming convention for classes in PEP8, more here: realpython.com/lessons/python-naming-conventions/

Become a Member to join the conversation.