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?

2
  • Why did you declare recordList as a star argument? Commented May 19, 2016 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()] ? Commented May 19, 2016 at 19:07

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

Just unpack it as you would with any other function.

func(*[...])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.