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
13
votes
3 answers

Equality overloading for namedtuple

Is there a way to overload the equality operator __eq__(self, other) for a namedtuple in python? I know this is possible in classes and redefining the method, but is this possible for a namedtuple as well, and how would you implement this?
EVR
  • 346
  • 2
  • 7
13
votes
1 answer

How named tuples are implemented internally in python?

Named tuples are easy to create, lightweight object types. namedtuple instances can be referenced using object-like variable deferencing or the standard tuple syntax. If these data structures can be accessed both by object deferencing & indexes,…
Vivek S
  • 5,384
  • 8
  • 51
  • 72
13
votes
2 answers

Serializing a nested namedtuple into JSON with Python >= 2.7

I have a problem similar to CalvinKrishy's problem Samplebias's solution is not working with the data I have. I am using Python 2.7. Here's the data: Namedtuple >>> a_t = namedtuple('a','f1 words') >>> word_t = namedtuple('word','f2 value') >>> w1 =…
Kaushik Acharya
  • 1,520
  • 2
  • 16
  • 25
13
votes
4 answers

What is the simple way to merge named tuples in Python?

I want to merge two namedtuples without loosing the key names. If, I just do a merge with '+' operator I am getting a tuple as a result but without the names. For instance: n [1]: from collections import namedtuple In [2]: A = namedtuple("A", "a b…
Senthil Babu
  • 1,243
  • 2
  • 11
  • 20
12
votes
1 answer

Why is a NamedTuple containing mutable objects hashable, when a Tuple containing mutable objects is not?

I understand why a tuple which contains a mutable object like list is not hashable, since list items in the tuple can still be updated. example: # hashable tuple_test = (1,2,3) print(tuple_test.__hash__()) While this is not hashable: # Not…
MaverickD
  • 1,477
  • 1
  • 13
  • 27
12
votes
2 answers

Python naming convention - namedtuples

I am new to Python and I have been reading both the online documentation and (trying) to follow PEP 0008 to have a good Python code style. I am curious about the code segment I found in the official Python docs while studying about the re…
user6003691
12
votes
1 answer

Why can't I pickle a typing.NamedTuple while I can pickle a collections.namedtuple?

Why can't I pickle a typing.NamedTuple while I can pickle a collections.namedtuple? How can I manage to do pickle a NamedTuple? This code shows what I have tried so far: from collections import namedtuple from typing import NamedTuple PersonTyping…
marcotama
  • 1,991
  • 2
  • 19
  • 24
12
votes
3 answers

Pythonic alternative to (nested) dictionaries with the same keys?

I find myself avoiding dictionaries because, often, nearly half their code is duplicate. I typically do this in nested dictionaries, where all sub-dictionaries contain the same keys, but different values. I manually create a large parent…
OnStrike
  • 748
  • 1
  • 6
  • 22
12
votes
3 answers

extend Python namedtuple with many @properties?

How can namedtuples be extended or subclassed with many additional @properties ? For a few, one can just write the text below; but there are many, so I'm looking for a generator or property factory. One way would be to generate text from _fields and…
denis
  • 21,378
  • 10
  • 65
  • 88
11
votes
4 answers

Equivalent of named tuple in NumPy?

Is it possible to create a NumPy object that behaves very much like a collections.namedtuple, in the sense that elements can be accessed like so: data[1] = 42 data['start date'] = '2011-09-20' # Slight generalization of what is possible with a…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
11
votes
4 answers

Should namedtuples follow constant name conventions in python?

I've written a small python module where I use a couple of namedtuples to pass info around because I find them very expressive. I considered these types and named them following the PEP8 convention for class names (CamelCased). However pylint sees…
Luis
  • 859
  • 2
  • 9
  • 20
10
votes
4 answers

nesting with namedtuple

I'm having trouble getting my data in the form that I'd like in python. Basically I have a program that reads in binary data and provides functions for plotting and analysis on said data. My data has main headings and then subheadings that could be…
Daniel Fletcher
  • 101
  • 1
  • 4
10
votes
1 answer

typing.NamedTuple with a dictionary default

I'm trying to make a NamedTuple where one field defaults to an empty dictionary. This mostly works, however the default value is shared between instances of the NamedTuple: from typing import NamedTuple, Dict class MyTuple(NamedTuple): foo:…
celion
  • 3,864
  • 25
  • 19
10
votes
1 answer

How to apply a special methods 'Mixin' to a typing.NamedTuple

I love the typing.NamedTuple in Python 3.6. But there's often the case where the namedtuple contains a non-hashable attribute and I want to use it as a dict key or set member. If it makes sense that a namedtuple class uses object identity (id() for…
Damon Maria
  • 1,001
  • 1
  • 8
  • 21
10
votes
1 answer

Python: Copying named tuples with same attributes / fields

I am writing a function that takes a named tuple and must return a super set of that tuple. For example if I was to receive a named tuple like this: Person(name='Bob', age=30, gender='male') I want to return a tuple that looks like…
Brian Crafton
  • 438
  • 2
  • 6
  • 15