You’ll start right away with this video course today about named tuples. We’ll link this topic with other lessons from earlier in the course later.
These lessons are part of a Real Python video course by Christopher Trudeau.
You’ll start right away with this video course today about named tuples. We’ll link this topic with other lessons from earlier in the course later.
These lessons are part of a Real Python video course by Christopher Trudeau.
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.
You must own this product to join the conversation.