By AP257


2010-08-13 19:40:06 8 Comments

I think 'unpack' might be the wrong vocabulary here - apologies because I'm sure this is a duplicate question.

My question is pretty simple: in a function that expects a list of items, how can I pass a Python list item without getting an error?

my_list = ['red', 'blue', 'orange']
function_that_needs_strings('red', 'blue', 'orange') # works!
function_that_needs_strings(my_list) # breaks!

Surely there must be a way to expand the list, and pass the function 'red','blue','orange' on the hoof?

3 comments

@vishes_shell 2016-09-04 19:11:35

Since Python 3.5 you can unpack unlimited amount of lists.

PEP 448 - Additional Unpacking Generalizations

So this will work:

a = ['1', '2', '3', '4']
b = ['5', '6']
function_that_needs_strings(*a, *b)

@answerSeeker 2017-10-05 02:12:36

how can I do the same with python 2.7 or 3.4?

@azalea 2019-01-02 18:48:17

@answerSeeker not efficient, but function_that_needs_strings(*(a+b))

@Martijn Pieters 2013-12-17 19:37:36

Yes, you can use the *args (splat) syntax:

function_that_needs_strings(*my_list)

where my_list can be any iterable; Python will loop over the given object and use each element as a separate argument to the function.

See the call expression documentation.

There is a keyword-parameter equivalent as well, using two stars:

kwargs = {'foo': 'bar', 'spam': 'ham'}
f(**kwargs)

and there is equivalent syntax for specifying catch-all arguments in a function signature:

def func(*args, **kw):
    # args now holds positional arguments, kw keyword arguments

@Jochen Ritzel 2010-08-13 19:40:59

function_that_needs_strings(*my_list) # works!

You can read all about it here.

@John Machin 2010-08-13 21:31:49

-1 Assuming that your csvfile is a csv.writer object, its writerow method has only one arg. *args in a function/method call is just a short way of writing arg[0], arg[1], .... The function or method must be capable of handling the args that you supply. *args is not voodoo that overrides the funtion/method arg declaration. Your example "works" only when len(my_list) == 1. This is the worst case of Gadarene upvoting that I've seen for a while.

@Jochen Ritzel 2010-08-13 22:41:41

@John Machin: You missed the point of the question. I just copied the function from the question's first revision - but I changed it just for you.

@John Machin 2010-08-13 23:04:27

@THC4k: I'm well aware of the point of the question. Where you copied parts of your answer from is irrelevant. My point is that the first sentence of your answer was ludicrously wrong.

@Jochen Ritzel 2010-08-13 23:23:59

Then I hereby declare that you were wrong by "assuming that your csvfile is a csv.writer object" :P I don't get the point of this discussion anyways.

@AP257 2010-08-15 20:07:41

yeah, @THC4k got the answer right - my bad for posting the wrong function in the question first time around...

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 1650028 View
  • 1707 Score
  • 11 Answer
  • Tags:   python list indexing

7 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3081113 View
  • 1783 Score
  • 7 Answer
  • Tags:   python list

39 Answered Questions

[SOLVED] How to make a flat list out of list of lists

25 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

20 Answered Questions

30 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2254828 View
  • 3237 Score
  • 30 Answer
  • Tags:   python list

21 Answered Questions

15 Answered Questions

[SOLVED] What are metaclasses in Python?

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3275879 View
  • 2697 Score
  • 28 Answer
  • Tags:   python list indexing

56 Answered Questions

[SOLVED] Calling an external command in Python

Sponsored Content