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

Give Feedback

In this lesson, you’ll see some of the problems you could run into if you use mutable data structures to store your data set. Then, you’ll see how you can approach this data set with immutable data sctrutures.

You’ll learn how you can use .namedtuple() from the collections module, which is built into Python, in order to represent your data in an immutable data structure so it can’t be modified in-place.

Comments & Discussion

MrMaul on Dec. 7, 2019

Which editor is being used in the video?

B S K Karthik on Feb. 27, 2020

Which IDE/Command prompt have you used.Can you please tell me.It looks good.

Dan Bader RP Team on Feb. 27, 2020

I’m using an alternative Python REPL called bpython in my videos. You can learn more about it here: bpython-interpreter.org

B S K Karthik on Feb. 28, 2020

Thank you

Lokman on March 26, 2020

Hi @Dan Bader, just wanna correct your article above collection.namedtulple to collection.namedtuple.

Dan Bader RP Team on March 27, 2020

Thanks for catching my typo there, should be fixed now :)

elliekafubu on March 28, 2020

is it possible to have a pdf of these wonderful lessons?

Choong Han on March 31, 2020

Hey, in the namedtuple function, what does the first index, scienctist represent

Varun Vaddiparty on May 10, 2020

@Han It is the typename. collections.namedtuple returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable.

pavanala13433 on July 6, 2020

hey i think we can change names of scientists using ada._replace(name=”xxx”) function so the named tuple is also mutable am i right once check the functionality of _replace function in namedtuple

pavanala13433 on July 6, 2020

hey dan can you please respond to my commment

Geir Arne Hjelle RP Team on July 6, 2020

@pavanala13433

_replace() creates a new named tuple with certain values replaced. It does not mutate the original named tuple:

>>> from collections import namedtuple
>>> Point = namedtuple("Point", ["x", "y"])
>>> p = Point(1, 2)
>>> p
Point(x=1, y=2)

>>> p._replace(x=3)
Point(x=3, y=2)

>>> p
Point(x=1, y=2)

pavanala13433 on July 6, 2020

Thank you very much sir @Geir Arne Hjelle for responding and cleared my doubt with your explanation.....

Jakob Fredriksson on Aug. 1, 2020

Began learning how to code this spring and this course is a bless! I do like Dan’s teaching quite much as well (His courses on YouTube that made me a paying member on Real Python).

However. Since start, I haven’t liked OOP for some reason… Since I grasped the concept somewhat - in my case barely removed the dust from the box - I’ve all the time the feeling of “passing the river to collect water”. Guess I don’t understand the concept enough.

Gah, now I’m loosing the thread. My intension is NOT going into any OOP debate (understand it’s a thing). What do I know. I just wanted to point out this style of coding appeals more to me =)

Dan Bader RP Team on Aug. 1, 2020

Thanks for your comment Jakob, glad to hear that :) The way I look at FP vs OOP is that they’re just different “tools in the tool box” and I try to pick whichever I find the most appropriate given the circumstances. And subjective appeal/enjoyment can be a great reason for choosing one style over another! (At least on hobby projects or projects that don’t have to stick to an already established architectural style.)

By the way, if you’re looking for resources to make Python OOP “click” then check out our Python OOP Learning Path here on Real Python!

Jakob Fredriksson on Aug. 2, 2020

Thx for the link to RP’s OOP Learning Path. I will grind the whole thing during upcoming weeks. Btw, it must be written:

“Give me all the x:es where x is in scientists but only if x.nobel is True”. The way you commented between syntaxes while writing this finally made me confident with list comprehensions.

It’s fascinating how effective tiny details sometimes are =)

Become a Member to join the conversation.