Technology3 min read

NamedTuple in Python - Part 1

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Python NamedTuple

Summarize this article using AI

In this post, we will understand what a NamedTuple in Python is, how it is different from other container structures and how it can be created.

Note: Remember that NamedTuple was introduced in Python 2.6 and can be used in the versions >=2.6 only.


What is a NamedTuple?

You might be familiar with a tuple, it is an immutable data structure that is used to store data elements in Python. As the name suggests, the NamedTuple is a tuple in which every value has a pre-assigned name associated with it. It is present in the collections module. It is a container and is similar to dictionaries because it contains a key that is associated with a specific data element/value (multiple values also can be mapped).

One significant difference between a NamedTuple and a dictionary is that NamedTuple can be accessed with the help of a key as well as iterating over the elements in the NamedTuple. This functionality hasn't been implemented in dictionaries as of now.




How to define a NamedTuple?

There are different ways to define a NamedTuple, few of them have been listed below:


1. As a string which is comma-separated

Since a key-value pair (similar to a dictionary) only can be passed to a NamedTuple, if the number of values associated with a specific namedtuple is more than one, it has to be passed as a string or in such a way with which it is interpreted as a single entity mapped to the key.

NamedTuple in Python

As shown in the image above, Website_details is the name of the namedtuple and that named tuple can store two values for the key, Name and Type.


Time for an example:

from collections import namedtuple

# initialising a named tuple
website = namedtuple('Website_details', "Name, Type")
# adding values to it
my_website = website('Studytonight', 'Student friendly')
print(my_website)

Output:

Website_details(Name='Studytonight', Type='Student friendly')



2. Using a list for keys

The value associated with the namedtuple can be passed as a list as well, when the number of keys associated with a namedtuple is greater than 1 (usually, this is the case, otherwise, dictionaries could be used).

NamedTuple in Python


Time for an example:

from collections import namedtuple

website = namedtuple('Website_details', ['Name', 'Type', 'number_of_characters'])
my_website = website( 'Studytonight', 'Student friendly', 12)
print(my_website)

Output:

Website_details(Name='Studytonight', Type='Student friendly', number_of_characters=12)



3. Passing Keys as a string which is space-separated

The key bound with a namedtuple can also be passed as a string that is separated by spaces.

NamedTuple in Python


Time for an example:

from collections import namedtuple

website = namedtuple('Website_details', 'Name Type number_of_characters')
my_website = website( 'Studytonight', 'Student friendly', 12)
print(my_website)

Output:

Website_details(Name='Studytonight', Type='Student friendly', number_of_characters=12)



4. Using a dictionary

The keys associated with a namedtuple can be passed as dictionary elements which can later be mapped to values.

NamedTuple in Python


Time for an example:

from collections import namedtuple

website = namedtuple('Website_details', {'Name':'', 'Type':'', 'number_of_characters':''})
my_website = website( 'Studytonight', 'Student friendly', 12)
print(my_website)

Output:

Website_details(Name='Studytonight', Type='Student friendly', number_of_characters=12)



Conclusion:

In this post, we saw what a NamedTuple is, and how it is different from a dictionary. We also saw how a NamedTuple can be defined in different ways. In the next tutorial covering NamedTuple we will cover some useful methods associated with namedtuple.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next