0

I have a class that looks like this:

class imported_feature():
    def function_print(self, *recordList):
        """Print the first three argument of the list"""
        for i in range(3):
            print(recordList[i])

and I want to call the function with something that looks like this:

imported_feature.function_print([columns_to_add[x][j] for x in columns_to_add.keys()])

In columns_to_add I have all the objects that I want to give the function_print(). I iterate the function with "j" (I want to send the "j" element of each key to function_print)

I know how to solve it while making changes in function_print() (change recordList[i] to recordList[0][i]) but I wan't another solution where I change the input to be recognized as multiple objects instead of just one object?

I have tried to do:

list =[columns_to_add[x][j] for x in columns_to_add.keys()]
imported_feature.function_print(str(list)[1:-1])

But that didn't do it, any other tips on how I can unpack the keys in columns_to_add and insert the?

axel_ande
  • 359
  • 1
  • 4
  • 20
  • Why did you declare `recordList` as a star argument? – user2357112 May 19 '16 at 19:05
  • Just to clarify, if `columns_to_add` is a `dict`, instead of iterating over the keys just to grab the `j` value, why not `myList = [val[j] for val in columns_to_add.values()]` ? – dwanderson May 19 '16 at 19:07

2 Answers2

1

Just use * again:

lst = [columns_to_add[x][j] for x in columns_to_add.keys()]
imported_feature.function_print(*lst[1:-1])

(I don't recommend using keywords as variable names)

Anthon
  • 69,918
  • 32
  • 186
  • 246
1

Just unpack it as you would with any other function.

func(*[...])
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358