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
0
votes
2 answers

Inherited NamedTuple class with Factory

I have a class for decoding binary data using struct and storing in a NamedTuple as below: class HEADER1(NamedTuple): name: str u2: int tracetime: int u4: int u5: int u6: int u7: int struct = Struct('<8s6L') …
mrkbutty
  • 489
  • 1
  • 5
  • 13
0
votes
1 answer

What is the fastest way to find the average for a list of tuples in Python, each tuple containing a pair of namedtuples?

import numpy as numpy from collections import namedtuple from random import random Smoker = namedtuple("Smoker", ["Female","Male"]) Nonsmoker = namedtuple("Nonsmoker", ["Female","Male"]) LST =…
Paw in Data
  • 1,262
  • 2
  • 14
  • 32
0
votes
1 answer

Using collections.namedtuple with ProcessPoolExecutor gets stuck in a few cases

>>> import concurrent.futures >>> from collections import namedtuple >>> #1. Initialise namedtuple here >>> # tm = namedtuple("tm", ["pk"]) >>> class T: ... #2. Initialise named tuple here ... #tm = namedtuple("tm", ["pk"]) ... def…
0
votes
0 answers

Foreach Loop only looping once over a list containing namedtuples

I have this piece of code from collections import namedtuple Device = namedtuple('Device', ['Name', 'Timestamp']) #add samples deviceList = [Device(Name = "alo", Timestamp="00000"), Device(Name = "lilo", Timestamp="1111"), Device(Name = "piko",…
0
votes
1 answer

Adding a new property to namedTuple instance in python and convert to dict

I am trying to add a new property to a named tuple in which I want to store some values. Post that I want to convert it back to JSON to pass it as a parameter in api. I found an answer to add a property to a namedTuple however it creates a type and…
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
0
votes
1 answer

How to retrieve the fields of a namedtuple class (not instance)?

Given a namedtuple: from collections import namedtuple MyNamedTuple = namedtuple('MyNamedTuple', ['foo','bar']) I would like get an iterable over the fields of MyNamedTuple . Currently I do [k for k in MyNamedTuple._fields] but this uses the…
JuanPi
  • 789
  • 1
  • 6
  • 19
0
votes
1 answer

Are python's NamedTuple return structures one of the few places where mutable defaults should be used?

Methods to return structures from python functions have been discussed at length in various posts. Two good ones here and here. However, unless I have missed it, none of the proposed solutions define the structure in the same place where its members…
OldSchool
  • 459
  • 1
  • 3
  • 14
0
votes
0 answers

Writing namedtuple and grouping by name to CSV python

I am trying to write a grouped namedtuple to a CSV, where each line is each name The namedtuple is like this: [FullIndividualResults(Position='48', Cat='B', Name='John Smith ', Team='Team 1', Points='10') My intended output in the CSV would…
PythonIsBae
  • 368
  • 3
  • 10
0
votes
0 answers

append a list of named tuple with another tuple

I have created a namedtuple using below supply_nodes_list=list(df_supply_nodes.itertuples(name='supplynode', index=False)) Output of the above code is as follows. [supplynode(Supply_nodes=1, capacities=15), supplynode(Supply_nodes=2,…
suresh_chinthy
  • 377
  • 2
  • 12
0
votes
0 answers

NamedTuple indexing not supported by mypy?

I define a NamedTuple as follow to get data scraped from a webpage (grades from a restaurant rating website): class Grades(NamedTuple): Value: Optional[int] = None Price: Optional[int] = None Service: Optional[int] = None Taste:…
user2969402
  • 1,221
  • 3
  • 16
  • 26
0
votes
4 answers

#Python Why do I keep getting namedtuple attribute error for this code?

When I run the code as below, it returns attribute error. AttributeError: 'Contact' object has no attribute 'find_info' How should I fix this?? phonebook = {} Contact = namedtuple('Contact', ['phone', 'email', 'address']) def…
Alex Hu
  • 37
  • 7
0
votes
1 answer

Can I use `namedtuple` to give names to the elements of a 1D array?

res.x is an array I obtained by optimizing an objective function, and I want to give each element a name so I can use them more conveniently later. My initial idea is to use namedtuple, but apparently Python would just take the array as the first…
Paw in Data
  • 1,262
  • 2
  • 14
  • 32
0
votes
1 answer

How to initialize a NamedTuple child class different ways based on input arguments?

I am building a typing.NamedTuple class (see typing.NamedTuple docs here, or the older collections.namedtuples docs it inherits from) that can accept different ways of being initialized. Why NamedTuple in this case? I want it to be immutable and…
LightCC
  • 9,804
  • 5
  • 52
  • 92
0
votes
1 answer

Enforcing selected immutability in Python

I'd like to be able to declare specific variables and objects immutable. (The problem arises when object instance variables, which should be treated as immutable, can be changed.) One thought is to use named tuples. from collections import…
RussAbbott
  • 2,660
  • 4
  • 24
  • 37
0
votes
0 answers

What's the Pythonic Way to Build Elegant Dataclasses Dynamically (Or Not Dataclasses At All)?

I'm building a library to read from input and build fairly high level libraries for serialization/unserialization. The inputs will be well structured, but could change and/or be customized, so pre-building all the class types is not possible.…
aronchick
  • 6,786
  • 9
  • 48
  • 75