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
30
votes
1 answer

Subclassing collections namedtuple

Python's namedtuple can be really useful as a lightweight, immutable data class. I like using them for bookkeeping parameters rather than dictionaries. When some more functionality is desired, such as a simple docstring or default values, you can…
BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79
26
votes
2 answers

How do I create pandas DataFrame (with index or multiindex) from list of namedtuple instances?

Simple example: from collections import namedtuple import pandas Price = namedtuple('Price', 'ticker date price') a = Price('GE', '2010-01-01', 30.00) b = Price('GE', '2010-01-02', 31.00) l = [a, b] df = pandas.DataFrame.from_records(l,…
MikeRand
  • 4,788
  • 9
  • 41
  • 70
25
votes
4 answers

Why doesn't the namedtuple module use a metaclass to create nt class objects?

I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string.…
Rick
  • 43,029
  • 15
  • 76
  • 119
25
votes
4 answers

how do I add fields to a namedtuple?

I am working with a list of namedtuples. I would like to add a field to each named tuple after it has already been created. It seems I can do that by just referencing it as an attribute (as in namedtuple.attribute = 'foo'), but then it isn't added…
rudivonstaden
  • 7,675
  • 5
  • 26
  • 41
22
votes
1 answer

What are “named or labeled tuples” in Typescript?

Reading the changes in Typescript 4.0, I found the new feature: Labeled Tuple Elements I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). I don't expected they could be indexed both…
zerocewl
  • 11,401
  • 6
  • 27
  • 53
22
votes
2 answers

Python syntax for namedtuple inside a namedtuple

Is it possible to have a namedtuple inside another namedtuple? For example: from collections import namedtuple Position = namedtuple('Position', 'x y') Token = namedtuple('Token', ['key', 'value', Position]) which gives a "ValueError: Type names…
Yannis
  • 1,682
  • 7
  • 27
  • 45
22
votes
1 answer

What's the first argument of namedtuple used for?

We use namedtuple like this: >>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y']) >>> p=Point(1,2) >>> p.x 1 I found the first argument of namedtuple seems useless, since: Firstly, we can not use it (to create an…
WKPlus
  • 6,955
  • 2
  • 35
  • 53
21
votes
1 answer

Understanding subclassing of JSONEncoder

I am trying to subclass json.JSONEncoder such that named tuples (defined using the new Python 3.6+ syntax, but it probably still applies to the output of collections.namedtuple) are serialised to JSON objects, where the tuple fields correspond to…
Xophmeister
  • 8,884
  • 4
  • 44
  • 87
21
votes
2 answers

Modifying a namedtuple's constructor arguments via subclassing?

I want to create a namedtuple which represents the individual flags in a short bitfield. I'm trying to subclass it so that I can unpack the bitfield before the tuple is created. However, my current attempt isn't working: class…
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
19
votes
2 answers

Is `namedtuple` really as efficient in memory usage as tuples? My test says NO

It is stated in the Python documentation that one of the advantages of namedtuple is that it is as memory-efficient as tuples. To validate this, I used iPython with ipython_memory_usage. The test is shown in the images below: The test shows…
Ammar Alyousfi
  • 4,112
  • 5
  • 31
  • 42
17
votes
5 answers

Mapping result rows to namedtuple in python sqlite

I am playing a bit with the python api for sqlite3, i have a little table for store languages with an id, name and creation_date fields. I am trying to map the raw query results into a namedtuple as the docs recommend, it that way i can manage rows…
angvillar
  • 1,074
  • 3
  • 10
  • 25
15
votes
3 answers

In Python, how do I call the super class when it's a one-off namedtuple?

So, I have a large number of message Payload classes for a serial API, each of which has a number of immutable fields, a parse method, and some methods which are shared. The way I'm structuring this is that each will inherit from a namedtuple for…
mikepurvis
  • 1,568
  • 2
  • 19
  • 28
15
votes
2 answers

NamedTuple declaration and use in a single line

I would like to store a dimension namedtuple (x, y). I will only need this once in my entire program. I could do: Dimension = namedtuple('Dimension', ['x', 'y']) dim = Dimension(2, 3) but, since I'm sure this is the only Dimension I will need in…
Juicy
  • 11,840
  • 35
  • 123
  • 212
14
votes
5 answers

Make namedtuple accept kwargs

If I have a class like: class Person(object): def __init__(self, name, **kwargs): self.name = name p = Person(name='joe', age=25) # age is ignored Extra params are ignored. But if I have a namedtuple, I'll get `unexpected keyword…
Sam R.
  • 16,027
  • 12
  • 69
  • 122
13
votes
3 answers

Unwrap F# single-case discriminated union tuple type

We can unwrap type like type Address = Address of string using unwrapping function like let unwrapAddress (Address a) = a let addr = Address "sdf" let str = unwrapAddress addr so str will be of type string, but if there is type like this approach…
Shishkin Pavel
  • 351
  • 2
  • 19
1 2
3
36 37