Tags: call, dlike, example, flatten, function, programming, python, transform1, tuple, tuples

Flatten a list/tuple and Call a function with tuples

On Programmer » Python

73,894 words with 25 Comments; publish: Fri, 04 Jan 2008 18:12:00 GMT; (20093.75, « »)

Hi,

I am wondering how do I 'flatten' a list or a tuple? For example, I'd

like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4].

Another question is how do I pass a tuple or list of all the

aurgements of a function to the function. For example, I have all the

arguments of a function in a tuple a=(1,2,3). Then I want to pass each

item in the tuple to a function f so that I make a function call

f(1,2,3). In perl it is a given, but in python, I haven't figured out

a way to do it. (Maybe apply? but it is deprecated?)

Thanks,

cg

All Comments

Leave a comment...

  • 25 Comments
    • On Jul 25, 9:50 am, beginner <zyzhu2....python.todaysummary.com.gmail.comwrote:
      Quote:
      === Original Words ===

      Hi,

      >

      I am wondering how do I 'flatten' a list or a tuple? For example, I'd

      like to transform[1, 2, (3,4)] or [1,2,[3,4]] to [1,2,3,4].

      >

      Another question is how do I pass a tuple or list of all the

      aurgements of a function to the function. For example, I have all the

      arguments of a function in a tuple a=(1,2,3). Then I want to pass each

      item in the tuple to a function f so that I make a function call

      f(1,2,3). In perl it is a given, but in python, I haven't figured out

      a way to do it. (Maybe apply? but it is deprecated?)

      >

      Thanks,

      cg

      I'm not sure about the first question, but as for the second, you

      could do a few different things. You could just pass the tuple itself

      into the function and have the tuple unpacked inside the function.

      <code>

      f(a)

      def f (tuple):

      x,y,z = tuple

      </code>

      You could also pass the elements of the tuple in:

      f(a[0], a[1], a[2])

      That would do it the way you describe in your post.

      Mike

      #1; Fri, 04 Jan 2008 18:13:00 GMT
    • On Wed, 25 Jul 2007 14:50:18 +0000, beginner wrote:

      Quote:
      === Original Words ===

      Thanks,

      cg

      HTH,

      Stargaming

      #2; Fri, 04 Jan 2008 18:14:00 GMT
    • On Jul 25, 10:19 am, Stargaming <stargam....python.todaysummary.com.gmail.comwrote:
      Quote:
      === Original Words ===

      Thanks,

      cg

      >

      HTH,

      Stargaming

Hi Stargaming,

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.

Do you know how to get that to work?

Thanks,

cg

#3; Fri, 04 Jan 2008 18:15:00 GMT
  • As I mentioned, you can access the elements individually using square

    brackets. The following works:

    f(g()[0], g()[1], 10)

    But it's not clear. Unfortunately, I'm not seeing much else for tuple

    unpacking except the obvious:

    a,b=g()

    f(a,b,10)

    Mike

    #4; Fri, 04 Jan 2008 18:16:00 GMT