Signup/Sign In

NamedTuple in Python - Part 1

Posted in Technology   LAST UPDATED: DECEMBER 3, 2019

    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.

    Author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    PythonPython NamedTuple
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS