Flatten a list/tuple and Call a function with tuples

B

beginner

beginner said:
I know the * operator. However, a 'partial unpack' does not seem to work.

A few other posters have mentioned ways around this, but you might ask
yourself what coding situation makes you want to do this stuff in the
first place. I won't say there's never a reason for it, but a lot of
times, a list containing a mixture of scalars and lists/tuples is a
sign that your underlying data representation is contorted. Things
are logically single values or they are logically lists of values, and
that mixed representation is often a sign that the item logically
should be a list, and you're hairing up the program with special
treatment of the case where the list has exactly one element.

I.e. instead of [[1,2,], 3, [5,6,]] maybe you really want
[[1,2,], [3,], [5,6]] without the special treatment and flattening.

Very good question. It is well possible that the problem is my
programming style. I am new to python and am still developing a style
that works for me. A lot of things that work in perl does not seem to
work. Unpacking and flattening are just two examples.

I need nested lists to represent nested records in a script. Since the
structure of the underlying data is nested, I think it is probably
reasonable to represent them as nested lists. For example, if I have
the below structure:

Big Record
Small Record Type A
Many Small Record Type B
Small Record Type C

It is pretty natural to use lists, although after a while it is
difficult to figure out the meaning of the fields in the lists. If
only there were a way to 'attach' names to members of the list.

For the unpacking question, I encountered it when working with list
comprehensions. For example:

[ f(*x,1,2) for x in list] is difficult to do if I don't want to
expand *x to x[0]..x[n]. There are usually 7-10 items in the list and
it is very tedious and error prone.

The second problem is from a nested list comprehension. I just needed
something to flatten the list at the moment.

I am still forming my way to do things in python via trial and error.
It is well possible that this is not the natural way to do things.
 
B

beginner

Also, this has not been suggested:
py> def g():
... return (1,2)
...
py> def f(a,b,c):
... return a+b+c
...
py> f(c=10, *g())
13

James- Hide quoted text -

- Show quoted text -

Great idea.
 
B

beginner

Well, there are several ways to solve this. You could either invoke f(*g
() + (10,)). Might be a bit nasty and unreadable, though. Or you could
just change your function f to accept them in reversed order (f(10, *g)
should work) or convert g() to return a dictionary like {'b': 1, 'c': 2}
and use f(10, **g). But if your function f's hugest use case is being
called with g(), changing f to accept something and g's result (tuple) --
unpacking it inside f -- might be better.- Hide quoted text -

- Show quoted text -

These all work. Thanks.
 
J

Jeff

For example, if I have
the below structure:

Big Record
Small Record Type A
Many Small Record Type B
Small Record Type C

It is pretty natural to use lists, although after a while it is
difficult to figure out the meaning of the fields in the lists. If
only there were a way to 'attach' names to members of the list.

You could use dictionaries:

big_record = {
"small_record_a": { ... },
"many_small_record_b": {
"sub_record_of_b": { ... },
"sub_record_of_b2": { ... },
},
"small_record_c": { ... },
}
 
S

Steven D'Aprano

I know the * operator. However, a 'partial unpack' does not seem to
work.

def g():
return (1,2)

def f(a,b,c):
return a+b+c

f(*g(),10) will return an error.


No it doesn't, it _raises_ an exception. This is a function that returns
an error:

def function():
"""Returns an error."""
return Error() # defined elsewhere

But to answer your question:
Do you know how to get that to work?

It's a little bit messy, but this works:
13

This is probably easier to read:
13
 
S

Steven D'Aprano

Things
are logically single values or they are logically lists of values

Except for strings, and string-like objects.

And files.

And records/structs, and tuples.

And lists. And sets.

And bit strings. And tree-like structures.

And, well, just about everything really.

But apart from those minor exceptions, I agree completely with your
recommendation.

(Ha ha only serious.)
 
M

Marc 'BlackJack' Rintsch

I need nested lists to represent nested records in a script. Since the
structure of the underlying data is nested, I think it is probably
reasonable to represent them as nested lists. For example, if I have
the below structure:

Big Record
Small Record Type A
Many Small Record Type B
Small Record Type C

It is pretty natural to use lists, although after a while it is
difficult to figure out the meaning of the fields in the lists. If
only there were a way to 'attach' names to members of the list.

That's where you may start looking into classes. The simplest one is for
just holding attributes seems to be the "bunch":

In [15]: class Bunch(object):
....: def __init__(self, **kwargs):
....: self.__dict__ = kwargs
....:

In [16]: small_a = Bunch(foo=42, bar=23)

In [17]: many_b = [1, 2, 3]

In [18]: small_c = Bunch(name='eric', profession='viking')

In [19]: big_record = Bunch(small_a=small_a, many_b=many_b, small_c=small_c)

In [20]: big_record.small_a.bar
Out[20]: 23

In [21]: big_record.many_b[1]
Out[21]: 2

For the unpacking question, I encountered it when working with list
comprehensions. For example:

[ f(*x,1,2) for x in list] is difficult to do if I don't want to
expand *x to x[0]..x[n]. There are usually 7-10 items in the list and
it is very tedious and error prone.

If you are the designer of `f` then just receive the whole `x` as *one*
argument.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,171
Latest member
VinayKumar Nevatia__
Top