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

Sort a list of namedtupled by the most frequent fields

What are some elegant and quick easy ways to sort a list of namedtuple by the most frequent elements in the list? For example, we have this list character_list = [ Element(id=1, character='A'), Element(id=2, character='B'), Element(id=3,…
samxiao
  • 2,587
  • 5
  • 38
  • 59
0
votes
1 answer

Passing named tuple field name for a ._replace as an argument from a function

So I have a named tuple, say: from collections import namedtuple Symbol = namedtuple('Symbol', 'name code industry date_au open high low close volume weekday_au date_utc_unixtimestamp prev_volume', defaults = ('DEFAULT', 'DEF', 'DEFAULT',…
user3674993
  • 129
  • 1
  • 9
0
votes
1 answer

Merge/Coalesce two namedtuples in Python

I have a namedtuple with some default values, and I'd like to merge (or field-wise coalesce) it with another instance of the same namedtuple T = collections.namedtuple("T", ("a", "b")) x = T(1, None) y = T(None, 1) z = coalesce(x, y) # same as z =…
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
0
votes
1 answer

How does nested named tuple equality check work?

import typing from enum import Enum class Group(typing.NamedTuple): group_id: int group_name: str class Groups(Enum): A = Group(1, 'Group A') B = Group(2, 'Group B') class Member(typing.NamedTuple): member_id: int …
variable
  • 8,262
  • 9
  • 95
  • 215
0
votes
2 answers

Create a nested data structure and fast accessing from another module while typing (Python)

Hello :) I'm using two modules in Python: Module 1 (for creating a nested data structure) and Module 2 (for accessing the "fields" of the structure and, very important, I want Python to show me the fields while I'm typing in Module 2 as there will…
dmg
  • 1
  • 1
  • 2
0
votes
1 answer

How to access the dynamically generated tuple in Python?

I have created a tuple for django model choices dynamically. The choices look like this [('cash', 'Pay via cash'), ('internet_banking', 'Pay via internet banking'), ('C2P', 'Pay via Credit Card')] Created it dynamically because the generated…
S.K
  • 480
  • 1
  • 4
  • 19
0
votes
1 answer

In Python, how to represent a class in the same way named tuple does?

I initially wrote some code, making use of a named tuple: from collections import namedtuple Count = namedtuple('Count', 'input_number multiple_of count remainder') def get_count(input_number: int, multiple_of: int) -> Count: ''' >>>…
dnk8n
  • 675
  • 8
  • 21
0
votes
0 answers

Python reformat namedtuple argument value

My code receives a json which I then turn into a dictionary. I want to create a class from that dictionary so that my IDE can check for a typo when the attributes are used. Moreover, I want to change the attribute called date to be of type date…
Ella Sharakanski
  • 2,683
  • 3
  • 27
  • 47
0
votes
2 answers

How to append row from itertuples to dataframe withouth losing the index in Python?

I have the following problem: I have a DataFrame df which looks like this: eqpid recpid queuetime trackintime trackouttime 3723 A XYZ 2017-01-01 03:14:58 2017-01-04 03:43:28 2017-01-04 03:43:33 ... ... …
0
votes
2 answers

Named tuple defined in one pyhton-module not recognised by another python module

I have defined the named-tuple in one function of the TLV.py module and return the same type in this function. But when I call the function defined in TLV.py in another module ISO8583.py, the named-tuple keys are not recognized. In my following…
Raj
  • 151
  • 1
  • 14
0
votes
1 answer

How do you index a list of namedtuples containing stock info given a range of date?

[Stock(date='2019-11-20', open='150.3100', high='150.8400', low='148.9600', close='148.9700', volume='11291822', indicator=0, signal='', change=0), Stock(date='2019-11-19', open='150.8800', high='151.3300', low='150.2000', close='150.3900',…
Phong Le
  • 13
  • 4
0
votes
2 answers

is it possible to get element from tuple of named tuples by key?

I defined this named tuple _validation_message = namedtuple("_validation_message", "validation msg") and use it as a value inside a regular tuple like this: _messages = ( _validation_message("mandatory", "mandatory field"), …
erez shaer
  • 11
  • 1
0
votes
1 answer

How to fix "__new__() missing 3 required positional arguments:.." error in Python

I'm working on a python code and I get this error: "TypeError: new() missing 3 required positional arguments: 'name', 'freq', and 'gen'" I'm importing a csv file to create a list of tuples, using a namedtuple. import csv from collections import…
Nero
  • 11
  • 2
0
votes
2 answers

When creating a namedtuple, how do I substitute a value?

I'm pulling data from the NHTSA API, using a JSON format. I'm then creating a named tuple from this data and a few other sources and using this as a record to insert into a MySQL database. The NHTSA API uses '' to designate a null value which is not…
0
votes
1 answer

Indexing a namedtuple nested in a dictionary

I'm looking to store a named tuple inside a dictionary. That parts easy. I don't know how to reference an individual bit in the namedtuple following that though. I know that I could just use a dictionary and make life easier, but in the case where…
cts
  • 3
  • 2