Questions tagged [namedtuple]

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

Documentation: collections module in Python 3.

Recently, a declarative API for namedtuples is also provided by the typing module.

545 questions
61
votes
7 answers

How to check if an object is an instance of a namedtuple?

How do I check if an object is an instance of a Named tuple?
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
57
votes
11 answers

Why does Python not support record type? (i.e. mutable namedtuple)

Why does Python not support a record type natively? It's a matter of having a mutable version of namedtuple. I could use namedtuple._replace. But I need to have these records in a collection and since namedtuple._replace creates another instance, I…
Salil
  • 9,534
  • 9
  • 42
  • 56
57
votes
1 answer

How to cast tuple into namedtuple?

I'd like to use namedtuples internally, but I want to preserve compatibility with users that feed me ordinary tuples. from collections import namedtuple tuple_pi = (1, 3.14, "pi") #Normal tuple Record = namedtuple("Record", ["ID", "Value",…
Adam Ryczkowski
  • 7,592
  • 13
  • 42
  • 68
56
votes
5 answers

Can't set attribute for subclasses of namedtuple

It looks like this or this are somewhat related threads, but still haven't figured things out :) I'm trying to create a subclass of namedtuple and provide different initializers so that I can construct objects in different ways. For example: >>>…
Jens
  • 8,423
  • 9
  • 58
  • 78
48
votes
2 answers

Getting name of value from namedtuple

I have a module with collection: import collections named_tuple_sex = collections.namedtuple( 'FlightsResultsSorter', ['TotalPriceASC', 'TransfersASC', …
user278618
  • 19,306
  • 42
  • 126
  • 196
46
votes
6 answers

What are the main differences of NamedTuple and TypedDict in Python / mypy

It seems to me that NamedTuple and TypedDict are fairly similar and the Python developers themselves recognized that. Concerning the PEP, I would rather add a common section about NamedTuple and TypedDict, they are quite similar and the latter…
Christoph
  • 26,519
  • 28
  • 95
  • 133
44
votes
3 answers

Relevance of typename in namedtuple

from collections import namedtuple Point = namedtuple('whatsmypurpose',['x','y']) p = Point(11,22) print(p) Output: whatsmypurpose(x=11,y=22) What's the relevance/use of 'whatsmypurpose'?
Phoenix
  • 4,386
  • 10
  • 40
  • 55
41
votes
4 answers

Inheriting from a namedtuple base class

This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b,…
alvas
  • 115,346
  • 109
  • 446
  • 738
41
votes
3 answers

Looping over elements of named tuple in python

I have a named tuple which I assign values to like this: class test(object): self.CFTs = collections.namedtuple('CFTs', 'c4annual c4perren c3perren ntfixing') self.CFTs.c4annual = numpy.zeros(shape=(self.yshape,…
user308827
  • 21,227
  • 87
  • 254
  • 417
40
votes
2 answers

What's the difference between enum and namedtuple?

I would like to know what are the differences between enum and namedtuple and when one should use one over the other.
Carlos Afonso
  • 1,927
  • 1
  • 12
  • 22
38
votes
3 answers

What is the pythonic way to read CSV file data as rows of namedtuples?

What is the best way to take a data file that contains a header row and read this row into a named tuple so that the data rows can be accessed by header name? I was attempting something like this: import csv from collections import namedtuple with…
drbunsen
  • 10,139
  • 21
  • 66
  • 94
37
votes
3 answers

Did something about `namedtuple` change in 3.5.1?

On Python 3.5.0: >>> from collections import namedtuple >>> cluster = namedtuple('Cluster', ['a', 'b']) >>> c = cluster(a=4, b=9) >>> c Cluster(a=4, b=9) >>> vars(c) OrderedDict([('a', 4), ('b', 9)]) On Python 3.5.1: >>> from collections import…
Nick Chammas
  • 11,843
  • 8
  • 56
  • 115
32
votes
2 answers

A way to subclass NamedTuple for purposes of typechecking

I have several namedtuples that share some fields. I have a function that accepts these tuples and is guaranteed to only interact with the shared fields. I want to typecheck such code in mypy. An example of the code would be: from typing import…
wuzwm
  • 501
  • 1
  • 5
  • 11
32
votes
3 answers

Pretty print namedtuple

I tried pprint from pprint, but its output is just one line, there is no multiline output and no indentation.
bobzhang
  • 1,771
  • 3
  • 17
  • 19
31
votes
3 answers

Python: Extending a predefined named tuple

I have the following named tuple: from collections import namedtuple ReadElement = namedtuple('ReadElement', 'address value') and then I want the following: LookupElement = namedtuple('LookupElement', 'address value lookups') There is duplication…
Har
  • 3,727
  • 10
  • 41
  • 75
1
2
3
36 37