Python Forum
how to get the keys in a named tuple
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get the keys in a named tuple
#1
i know how to get the keys from s dictionary. but how to do that for a named tuple?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
look at namedtuple._fields property`

from collections import namedtuple

Spam = namedtuple('Spam', 'foo, bar')

eggs = Spam(1, 2)
print(eggs._fields)
print(Spam._fields)
Output:
('foo', 'bar') ('foo', 'bar')
Also, FYI, you can get namedtuple as dict with _asdict() method
print(eggs._asdict())
Output:
OrderedDict([('foo', 1), ('bar', 2)])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The above and other useful things can be found in the documentation. I'd really recommend familiarising yourself with the library reference ("keep this under your pillow", as the Python docs page says).
Reply
#4
i do not keep it under my pillow. it keep it out where i can read it. and i do. it's just not indexed in a lookup manner by concept. it's more of a sequential definition. i still look for things but i don't spend an unlimited time doing that. at some point in time, when i have not found something, i have to move on, or use an associative memory ... asking others who may already know.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Mar-24-2020, 05:29 PM)Skaperen Wrote: it's just not indexed in a lookup manner by concept.

C'mon, really in this case you are not right. You ask about namedtuple. First thing you can do is check the docs and it's clear where you should look. It's not that long read. If you just have made the effort...
I've answered, no problem, but one should try....
Maybe you are confusing the Language reference and the Library refernce

and of course there is the official Python tutorial and the Python HOWTOs
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
i looked for it in the Library Reference where i do find lots of stuff but miss many things. i think it might be a failure to recognize with fast reading. i probably need to slowdown.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert a named tuple to a dictionary Skaperen 13 3,388 Mar-31-2022, 07:13 PM
Last Post: Skaperen
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,727 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,119 May-21-2019, 11:39 AM
Last Post: avorane
  Newbie question for sum the specific element in named tuple zydjohn 1 4,168 Dec-12-2017, 07:29 PM
Last Post: buran
  Newbie question for sorting named tuple list zydjohn 3 7,690 Dec-11-2017, 10:13 PM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020