Skip to main content

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
1 vote
0 answers
83 views

Searching a List of NamedTuples for Specific Attribute [duplicate]

I have a list of NamedTuples (extract below) and I want to search through the list and print "Found" if an attribute of one of the tuples contains a specific string. This is my first time ...
Andy's user avatar
  • 521
2 votes
0 answers
286 views

how to model simple nested data static structure instead of nested dictionaries (namedtuple, dataclass... )

I hope there is some best practice or design pattern for use case of having simple nested data/mapping with need to pre-store data, select proper data group, and conveniently access specific values. ...
stam's user avatar
  • 330
5 votes
1 answer
5k views

Can I unpack/destructure a typing.NamedTuple?

This is a simple question so I'm surprised that I can't find it asked on SO (apologies if I've missed it), and it always pops into my mind as I contemplate a refactor to replace a tuple by a ...
Cai's user avatar
  • 1,929
0 votes
2 answers
234 views

Are pickle-able tuple-factories (with names) possible?

There are several questions about pickling namedtuples already, however none of the ones I found [1] [2] [3] [4] deals with the case of pickling a namedtuple that is bound on an object instance. ...
Hyperplane's user avatar
  • 1,644
0 votes
1 answer
231 views

Mypy does not detect errors regarding the use of NamedTuple inside functions

In the following code the defintions of x2 and x3 are obviously wrong, yet mypy does not complain. from typing import NamedTuple class X(NamedTuple): a: float b: float c: float def foo():...
Friedrich Gretz's user avatar
1 vote
0 answers
44 views

Iterate Over List and Deduct Random Value from Time Values

I have a long list of NamedTuples which contain time values. Extract below. I want to iterate over the list and deduct a random value from each time value. ie a different random number for each time ...
Robsmith's user avatar
  • 431
0 votes
0 answers
290 views

How to type a generic function with namedtuples?

I have a wrapper class for a database cursor like this: namedtuple_def = TypeVar("namedtuple_def") class Wrapper: def fetchall(defn: Optional[namedtuple_def] = None) -> List[...
ldrg's user avatar
  • 4,392
1 vote
1 answer
572 views

Name a nested typing.NamedTuple in the same outer class in Python

I have a problem in Python I can work around, but I'd really like to understand why it doesn't work the way I think it should. I have a class A which has nested typing.NamedTuples B and C. So far so ...
GoDontKnow's user avatar
0 votes
1 answer
121 views

Iterate through list of NamedTuples and add random value

I have a list of NamedTuples (short extract below) and wrote some code to add the same random value to each of the a and b values in the tuple. The list will vary in length. This works ok but I now ...
Robsmith's user avatar
  • 431
-1 votes
1 answer
132 views

NamedTuple as String data types most versatile

from collections import namedtuple Data=namedtuple('Data','name number age cgpa')#best to keep all data types as string def prettyprint(X,end="\n"): s="{0},{1},{2},{3}".format(...
Soham Patil's user avatar
0 votes
2 answers
54 views

Search substring in list of dictionaries of namedtuples keyed with an event type

I have created a list of dictionaries of named tuples, keyed with an event type. [{'EVENT_DELETE': DeleteRequestDetails(rid=53421, user='user1', type='EVENT_DELETE', reviewed=1, approved=1, completed=...
gosaultech's user avatar
9 votes
1 answer
5k views

NamedTuple with default values

I'm trying to use a function with a parameter of namedTuple which has default values. I tried this. Is that somehow possible? from typing import Optional, NamedTuple Stats = NamedTuple("...
user avatar
0 votes
0 answers
505 views

Python proccess pool executor and named tuple leads to unit test error -> "cannot pickle '_io.TextIOWrapper' object"

I am trying to use namedtuple along with process pool executor and when writing a unit test , i keep getting an error below The dut in the code represent the python file where i have the 2 functions ...
Nandeep Devendra's user avatar
1 vote
3 answers
330 views

list of namedtuples; how to calculate the sum of individual elements

Say I have namedtuple: Trade = namedtuple('Trade', ['Ticker', 'Date', 'QTY', 'Sell', 'Buy', 'Profit']) Is there any 'pythonic' way to do a one-liner sum of each 'summable' or 'selected' (QTY, Sell, ...
okkraj's user avatar
  • 13
0 votes
1 answer
176 views

UrlSplitResult: can't _replace fields

I'm trying to inject a basic-authentication into an url by split-inject-join: url = urllib.parse.urlsplit(url) new_url = url._replace(username=user, password=password) But I'm surprised about ...
xtofl's user avatar
  • 41.3k

15 30 50 per page
1 2 3
4
5
37