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

Why differences in class.__slots__ assignment via decorator vs. class body?

I'm working on a decorator to implement some behaviors for an immutable class. I'd like a class to inherit from namedtuple (to have attribute immutability) and also want to add some new methods. Like this ... but correctly preventing new attributes…
user48956
  • 14,850
  • 19
  • 93
  • 154
0
votes
1 answer

How does collections.namedtuple work?

I am studying the code of "namedtuple" in Python.(Python 3.6.3). I run the code : from collections import namedtuple,_iskeyword Point = namedtuple('Point', ['x', 'y'],rename=False,verbose=True) p = Point(2,3) print(p) and then the console print…
Sigma65535
  • 361
  • 4
  • 15
0
votes
1 answer

Neater way to write huge lists for creating named tuples with default values

I have a named tuple object defined with 63 different items in it. I use the _make function. I think this approach will work but need 4x63 more rows for the try except statements. There has to be a better way: AssetRow =…
gunslingor
  • 1,358
  • 12
  • 34
0
votes
2 answers

Filling the matrix with namedtuples

Why is the matrix filled with namedtuples like this? Incorrectly inserted indexes And how to fix it? from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) m, n = 3, 3 matrix = [[None] * n] * m for i in range(m): for j in…
Vartan
  • 3
  • 3
0
votes
0 answers

Python: Why is embedded tuple shared

I do not understand the following behavior of nested named tuples in Python (3.6): class Small(NamedTuple): item: float = 0.0 class Outer(NamedTuple): zz = Small(item=random()) When I create two instances of the Small tuple: a = Small() b =…
Jirka
  • 4,184
  • 30
  • 40
0
votes
0 answers

How to dynamically create namedtuple from a list of lists?

So I have a list of list called templist like this: [['C0', 1, 1], ['NA', 4, 5], ['NA', 3, 7], ['C3', 9, 3], ['NA', 8, 4], ['NA', 7, 5], ['C6', 6, 6], ['NA', 5, 10]] The way you create a namedtuple for the above list is: from collections import…
user8508347
0
votes
0 answers

python expression for namedtuple size

I came across the following expression in python. I believe the expression itself is legit and how it constructs actually makes sense. But still, from strongly typed background, I am wondering if people typically do that in python ... i.e. using the…
0
votes
2 answers

Store variables in dictionary for large data

I can print variables in python. for h in jl1["results"]["attributes-list"]["volume-attributes"]: state = str(h["volume-state-attributes"]["state"]) if aggr in h["volume-id-attributes"]["containing-aggregate-name"]: …
0
votes
1 answer

Why does namedtuple._make check the return value's length?

Overriding the length method on namedtuple objects in Python is a fair bit more tedious than you might expect. The naive approach, from collections import namedtuple class Rule(namedtuple('Rule', ['lhs', 'rhs'])): def __len__(self): …
iafisher
  • 938
  • 7
  • 14
0
votes
1 answer

List comprehension of namedtuples results in empty list

I've stumbled upon a problem I just can't seem to find an answer for. I am reading the contents of a csv file using csv.DictReader and constructing a list of namedtuples from that information. However, upon running it, the end result is an empty…
Jenez
  • 29
  • 8
0
votes
0 answers

Doing multiple sort parameters on namedTuple in python

I would like to sort a list of namedTuple base on 2 parameters. For example, my data has a list of tuples containing (ExampleTuples): Example(key=0,time=15) Example(key=1,time=50) Example(key=2,time=15) Example(key=4,time=3) I want to sort them…
user1179317
  • 2,693
  • 3
  • 34
  • 62
0
votes
2 answers

Returning namedtuple from function

I am just looking to understand why I get the following results From the following code def sendHTTP(httpStatus): status_code = 400 reason = "Unauthorized" httpStatus(Code=req.status_code,Desc=req.reason) return httpStatus if…
alexis
  • 1,022
  • 3
  • 16
  • 44
0
votes
2 answers

Slowness on iterating over namedtuple and dicts for defined pairs

This code runs quickly on the sample data, but when iterating over a large file, it seems to run slowly, perhaps because of the nested for loops. Is there any other reason why iterating over items in a defaultdict is slow? import…
0
votes
1 answer

Get Iterator Tuple from SQLite Query

I'm connecting to an SQLite database. When querying the database in python, it returns a tuple object. I prefer to work with namedtuple. The problem is I can't iterate through the object more than once. I'm not sure if the issue is with the way in…
user2951249
  • 87
  • 1
  • 2
  • 7
0
votes
1 answer

Convert python mutable namedlist to immutable namedtuple

Is there a way to convert or make a copy of a python mutable namedlist to an immutable namedtuple? EDIT based on the comments: I have a namedlist filled with values >>> from namedlist import namedlist >>> nl = namedlist('myList', 'a b c d') >>> L…
Phiplex
  • 139
  • 1
  • 13