Hint: You can adjust the default video playback speed in your account settings.

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.

Become a Member to join the conversation.