Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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.

Writing Clean, Pythonic Code With namedtuple (Overview)

Python’s collections module provides a factory function called namedtuple(), which is specially designed to make your code more Pythonic when you’re working with tuples. With namedtuple(), you can create immutable sequence types that allow you to access their values using descriptive field names and the dot notation instead of unclear integer indices.

If you have some experience using Python, then you know that writing Pythonic code is a core skill for Python developers. In this video course, you’ll level up that skill using namedtuple.

In this video course, you’ll learn how to:

  • Create namedtuple classes using namedtuple()
  • Identify and take advantage of cool features of namedtuple
  • Use namedtuple instances to write Pythonic code
  • Decide whether to use a namedtuple or a similar data structure
  • Subclass a namedtuple to provide new features

To get the most out of this course, you need to have a general understanding of Python’s philosophy related to writing Pythonic and readable code. You also need to know the basics of working with:

If you don’t have all the required knowledge before starting this video course, then that’s okay! You can stop and review the above resources as needed.


Sample Code (.zip)

4.2 KB


Course Slides (.pdf)

750.2 KB

00:00 Welcome to Writing Clean, Pythonic Code With Named Tuples. My name is Christopher, and I will be your guide. In this course, you’ll learn about named tuples. In particular, how to create them with the collections.namedtuple() factory, defining default values for your named tuple, using built-in utility methods and attributes, making your code more Pythonic by using named tuples, deciding on when to use named tuples vs other data structures, and how to extend named tuples.

00:34 Tuples are built-in Python data types that specify an ordered sequence of data attributes. Conceptually, they’re similar to lists, but unlike lists, tuples are immutable.

00:44 A named tuple is an extension to tuples that is found in the collections module. They add some features to tuples—for example, the parts of a named tuple are, well, named.

00:54 This means the data inside the tuple can be accessed like the attribute of a class, using dot notation. Each named tuple automatically comes with some utility methods and extra fields.

01:05 This allows you to do things like create new instances of an immutable object based on your existing one. And like tuples, named tuples are very memory efficient. In fact, named tuples use no more memory than regular tuples themselves.

01:19 At the core of it, being able to name a field in a tuple makes your code more readable. Consider these variables: person[2] and person.middle_name.

01:29 The second one is far easier to understand within the context of your code.

01:36 Next up, I’ll dive in and show you how to create and manipulate named tuples.

Become a Member to join the conversation.