Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:52:36+00:00 2026-05-31T21:52:36+00:00

The standard library namedtuple class looks to me like a way to make tuples

  • 0

The standard library namedtuple class looks to me like a way to make tuples more like dictionaries. How do namedtuples compare to dicts? When should we use them? Do they work with non-hashable types?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:52:38+00:00Added an answer on May 31, 2026 at 9:52 pm

    In dicts, only the keys have to be hashable, not the values. namedtuples don’t have keys, so hashability isn’t an issue.

    However, they have a more stringent restriction — their key-equivalents, “field names”, have to be strings.

    Basically, if you were going to create a bunch of instances of a class like:

    class Container:
        def __init__(self, name, date, foo, bar):
            self.name = name
            self.date = date
            self.foo = foo
            self.bar = bar
    
    mycontainer = Container(name, date, foo, bar)
    

    and not change the attributes after you set them in __init__, you could instead use

    Container = namedtuple('Container', ['name', 'date', 'foo', 'bar'])
    
    mycontainer = Container(name, date, foo, bar)
    

    as a replacement.

    Of course, you could create a bunch of dicts where you used the same keys in each one, but assuming you will have only valid Python identifiers as keys and don’t need mutability,

    mynamedtuple.fieldname
    

    is prettier than

    mydict['fieldname']
    

    and

    mynamedtuple = MyNamedTuple(firstvalue, secondvalue)
    

    is prettier than

    mydict = {'fieldname': firstvalue, 'secondfield': secondvalue}
    

    Finally, namedtuples are ordered, unlike regular dicts, so you get the items in the order you defined the fields, unlike a dict.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the C++ standard library's find algorithm like this: template<class T>
If for example I should not use standard library functions like printf() , putchar()
Declarations of auto_ptr from C++ Standard Library namespace std { template <class Y> struct
Does the Java standard library have any functional data structures, like immutable Sets, Lists,
We (should) know that C++ standard library containers, including std::string , are not meant
Is there python function in standard library like def cond(condition, true, false): if condition:
In the Ruby Standard Library we have the Singleton class: http://ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html We can make
Does the standard library have a BigInt class? (numbers with many digits held in
Is there something like this in any standard library (e.g. apache-commons, guava) ? public
I'm trying to monkeypatch a line in Net class in the standard library. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.