You must have used a tuple or a python dictionary in your program. Although they are very useful data structures, they have some drawbacks. In this we will study what is a namedtuple in python and how we can use it in place of a tuple or a dictionary.
Problems with tuples and dictionaries
Suppose you are working on creating a python application and you have to specify the color of the graphical interface. In such a case, you can use a tuple with (red, green, blue) value as follows.
color=(100,125,130)
But, here is a problem. Suppose your teammate reads your code and he doesn’t understand what the tuple in the above example signifies. It may be (red,green,blue) value or (hue, saturation, brightness) value according to him. To avoid such confusion, you can use a dictionary with red, green and blue as its key.
color={"red":100, "green":125, "blue": 130}
Again, when we are using a dictionary, we will have to create a dictionary for every component used in the graphical interface. This will make our code redundant and unreadable. To solve these issues, we can use a namedtuple in our python program. Let us discuss what it is and how we will use it.
What is a namedtuple in python?
You can think of a namedtuple as an intermediate data structure between a tuple and a dictionary. As the name suggests, a namedtuple in python is a tuple with named fields. Generally , we access the elements of a tuple using their indices. But in a namedtuple, we can specify the names of each index and then we can access the elements using the names.
Let us discuss this in detail in the following sections.
How to create a namedtuple in Python?
Namedtuple has been defined in the collections module. You can import it as follows.
from collections import namedtuple
After importing the namedtuple module, you can create a namedtuple using the namedtuple() constructor.
The namedtuple() constructor takes the name of the object class to be created and their field names as input arguments. It then creates a subclass of tuple in which the fields are named.
For example, we can define a “Color” namedtuple with field names as “red”,”green” and “blue” as follows.
Color = namedtuple("Color", ["red", "green", "blue"])
After defining the namedtuple subclass, we can create named tuples by giving the values for the fields as follows.
color=Color(100,125,130)
We can also access the value at different fields of a namedtuple using the attribute notation as follows.
from collections import namedtuple
Color = namedtuple("Color", ["red", "green", "blue"])
color = Color(100, 125, 130)
print("The namedtuple is:", color)
print("The red value is:", color.red)
print("The green value is:", color.green)
print("The blue value is:", color.blue)
Output:
The namedtuple is: Color(red=100, green=125, blue=130)
The red value is: 100
The green value is: 125
The blue value is: 130
If needed, you can also find the field names of a namedtuple. For this, you can use the _fields attribute that contains the field names as follows.
from collections import namedtuple
Color = namedtuple("Color", ["red", "green", "blue"])
color = Color(100, 125, 130)
print("The namedtuple is:", color)
print("The fields of the namedtuple are:", color._fields)
Output:
The namedtuple is: Color(red=100, green=125, blue=130)
The fields of the namedtuple are: ('red', 'green', 'blue')
Benefits of using a namedtuple in Python
There are several benefits of using namedtuples over tuples and dictionaries.
- Unlike tuples, Namedtuples allow indexing as well as field names. This makes our code more understandable.
- Unlike dictionaries, namedtuples are immutable and can be stored in a set. Also, Using a namedtuple instead of a dictionary requires us to write less code.
- Despite having these advantages, namedtuples use almost similar memory compared to a tuple.
How to modify a namedtuple?
A namedtuple is a subclass of tuple and is considered to be immutable. It can be stored in a set and shows every property of immutable objects. But, we can modify the values in a namedtuple. For this, we can use the _replace() method that takes a keyword argument with the field name and new value. After that it returns the modified namedtuple as follows.
from collections import namedtuple
Color = namedtuple("Color", ["red", "green", "blue"])
color = Color(100, 125, 130)
print("The original namedtuple is:", color)
color = color._replace(red=200)
print("The modified namedtuple is:", color)
Output:
The original namedtuple is: Color(red=100, green=125, blue=130)
The modified namedtuple is: Color(red=200, green=125, blue=130)
Conclusion
In this article, we have discussed about namedtuple in python. We also discussed its use and benefits over tuples and dictionaries. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.