namedtuple Questions

14

Solved

What are named tuples and how do I use them? When should I use named tuples instead of normal tuples, or vice versa? Are there "named lists" too? (i.e. mutable named tuples) For the la...
Bennie asked 3/6, 2010 at 23:50

2

Solved

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...
Departed asked 5/12, 2016 at 18:19

6

Solved

If I have a class like: class Person(object): def __init__(self, name, **kwargs): self.name = name p = Person(name='joe', age=25) # age is ignored Extra params are ignored. But if I have a na...
Gorgon asked 1/3, 2016 at 15:52

11

Solved

Is it possible to add a documentation string to a namedtuple in an easy manner? I tried from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) """ A point in 2D space """ # ...
Muncy asked 22/10, 2009 at 10:55

12

Solved

What is the recommended way of serializing a namedtuple to json with the field names retained? Serializing a namedtuple to json results in only the values being serialized and the field names bein...
Merchantable asked 6/5, 2011 at 4:34

7

Solved

Long story short PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple and typing.NamedTuple. And now I'm wondering how to s...
Blackguard asked 3/8, 2018 at 11:32

2

Solved

I had namedtuple variable which represents version of application (its number and type). But i want and some restriction to values: Version = namedtuple("Version", ["app_type", "number"]) version ...
Spectrogram asked 26/6, 2019 at 19:43

2

Solved

Simple example: from collections import namedtuple import pandas Price = namedtuple('Price', 'ticker date price') a = Price('GE', '2010-01-01', 30.00) b = Price('GE', '2010-01-02', 31.00) l = [a, ...
Teressaterete asked 8/6, 2013 at 23:37

6

It seems to me that NamedTuple and TypedDict are fairly similar and the Python developers themselves recognized that. Concerning the PEP, I would rather add a common section about NamedTuple and...
Hothouse asked 21/11, 2018 at 9:40

14

Solved

Can anyone amend namedtuple or provide an alternative class so that it works for mutable objects? Primarily for readability, I would like something similar to namedtuple that does this: from Came...
Convolute asked 26/3, 2015 at 22:56

1

Solved

After Python 3.6, we have typing.NamedTuple, which is a typed version of collections.namedtuple(), we can inherit it like a class: class Employee(NamedTuple): name: str id: int Compared with col...
Levorotatory asked 3/9, 2022 at 16:17

2

Solved

Is it possible to have a namedtuple inside another namedtuple? For example: from collections import namedtuple Position = namedtuple('Position', 'x y') Token = namedtuple('Token', ['key', 'valu...
Pentangular asked 3/4, 2016 at 11:51

23

Solved

I'm trying to convert a longish hollow "data" class into a named tuple. My class currently looks like this: class Node(object): def __init__(self, val, left=None, right=None): self.val = val se...
Buckthorn asked 5/7, 2012 at 19:16

5

Solved

It looks like this or this are somewhat related threads, but still haven't figured things out :) I'm trying to create a subclass of namedtuple and provide different initializers so that I can cons...
Interclavicle asked 8/10, 2013 at 20:17

4

Solved

I'm creating a dictionary d of one million of items which are tuples, and ideally I'd like to access them with: d[1634].id # or d[1634]['id'] d[1634].name # or d[1634]['name'] d[1634].isvalid # or ...
Lackaday asked 5/12, 2020 at 17:34

2

Solved

I'm looking for something very like namedtuples: >>> from collections import namedtuple >>> Party = namedtuple('Party', ['guests', 'location']) >>> p = Party(['Jed', 'Fr...
Ezekielezell asked 3/8, 2015 at 15:9

1

Solved

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("Stat...
Blacksnake asked 20/12, 2021 at 17:9

3

Solved

Consider following piece of code: from collections import namedtuple point = namedtuple("Point", ("x:int", "y:int")) The Code above is just a way to demonstrate as to what I am trying to achieve...

5

Solved

I was having trouble implementing namedtuple._replace(), so I copied the code right off of the documentation: Point = namedtuple('Point', 'x,y') p = Point(x=11, y=22) p._replace(x=33) print p ...
Arkhangelsk asked 30/1, 2010 at 0:3

1

Solved

I would like to convert a NamedTuple to a Dict in Julia. Say I have the following NamedTuple: julia> namedTuple = (a=1, b=2, c=3) (a = 1, b = 2, c = 3) I want the following: julia> Dict(zip(...
Subplot asked 28/10, 2021 at 14:17

3

Solved

I have a tiny class that extends a namedtuple, but the __dict__ property of its instances is always returning empty. Point = namedtuple('Point', 'x y') p1 = Point(20, 15) print(p1, p1.__dict__) # ...
Saiva asked 31/3, 2014 at 4:34

5

Solved

I am playing a bit with the python api for sqlite3, i have a little table for store languages with an id, name and creation_date fields. I am trying to map the raw query results into a namedtuple a...
Flophouse asked 2/5, 2013 at 10:29

9

Solved

I have a dictionary like: d = {'a': 1, 'b': 2, 'c': 3, 'd': 4} which I would like to convert to a namedtuple. My current approach is with the following code namedTupleConstructor = namedtuple('myN...
Anaplastic asked 11/5, 2017 at 16:40

2

Solved

Can someone explain why NamedTuples and immutable structs are separate instead of NamedTuples being an anonymous struct like there are anonymous functions function (x) x^2 end? They look like they ...
Hu asked 28/9, 2021 at 23:36

5

Solved

I can access elements of a named tuple by name as follows(*): from collections import namedtuple Car = namedtuple('Car', 'color mileage') my_car = Car('red', 100) print my_car.color But how can ...
Species asked 19/6, 2017 at 15:52

© 2022 - 2024 — McMap. All rights reserved.