0

I want a one-dimensional data structure in Python that allows elements to be named, and is also compatible with *args (and maybe **kwargs). Does such a thing exist?

# setup idea (doesn't work)

Tuple = namedtuple("Tuple", ["a", "*b", "c"])
t = Tuple(1, 2, 3, 4)

# desired behavior below

print(t.a)
# output:
# 1

print(t.b)
# output:
# [2, 3]

for x in t:
    print(x)
# output:
# 1
# 2
# 3
# 4

The structure should also support assignment.

t.c = 2
for x in t:
    print(x)
# output:
# 1
# 2
# 3
# 2
martineau
  • 119,623
  • 25
  • 170
  • 301
yadec
  • 131
  • 1
  • 1
    Sounds like you want something like a `NamedList`. There's isn't one in the standard library, but third-party implementations are available if you look for them (or you could roll-your-own or possibly use the `AttrDict` implementation in this [answer](https://stackoverflow.com/a/38034502/355230) of mine to another question. – martineau Oct 05 '20 at 19:08

1 Answers1

0

I will try to answer.

*args is a list.

**kwargs is a dict.

They are both are one-dimensional. But they could be multidimensional as well. Like, list of lists, dict of lists, etc.

Dict allows elements to be named.

For example:

a = {'name1': ['a', 'b'], 'name2': ['b', 'c']}

I would recommend you read about python built structure first here - https://docs.python.org/3/tutorial/datastructures.html

After reading rethink your question.

Good luck!

mrvol
  • 2,575
  • 18
  • 21
  • This does not allow looping through all elements of interest with a single for loop. One could write another flattening function to achieve the desired effect, but that feels like a hack more than a solution. – yadec Oct 05 '20 at 20:36