Dynamic arguments for Python functions [duplicate] - python

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?

This is what the * operator is for:
print zip(*squares)

Related

How to clone a list within itself? [duplicate]

This question already has answers here:
Repeating list in python N times? [duplicate]
(4 answers)
Closed 12 months ago.
suppose I have a list
a = [1,2,4,5]
I want to create another list b which clones a and adds it 3 times. So b is expected to be
b = [1,2,4,5,1,2,4,5,1,2,4,5]
How do I do this easily?
Methods using pandas or numpy is also welcome.
#just use the * operator
b=a*3

Python iterate through a list of tuples [duplicate]

This question already has answers here:
Find an element in a list of tuples
(10 answers)
How to check if all elements of a list match a condition?
(5 answers)
Closed 5 years ago.
I am new in Python and I was curious if I can do something like this:
Let's say that I have a list of tuples and one variable like this:
list = [(123,"a"),(125,"b")]
variable = (123,"c")
Is it possible to search for the first element of the variable in the list like this?
if variable[0] in list[0]:

Call a function inside another function call [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 6 years ago.
def func1(arg1,arg2,arg3,arg4)
...
def func2(arg5,arg6)
return a,b,c,d
func1(func2(arg5,arg6))
Can I call func1(func2(arg5,arg6)) like this??
as func2 will return 4 items that'll be passed to func1
You would have to unpack the arguments, but yes you can do that using the * operator.
func1(*func2(arg5,arg6))

how to define a list with predefined length in Python [duplicate]

This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.

Calling itertools.product with unkown number of args [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 9 years ago.
I hope this is a easy one.
Is there any possiblity to call itertools.product with a yet (not hardcoded) not knwon number of arguments?
Something like this:
itertools.product(x[0],x[1],x[2],....)
and the dimension of x can't be hardcoded
Thanks!
Try:
itertools.product(*x)
i.e. we unpack the argument list.
You can use
itertools.product(*x)
Lookup *args and **kwargs?
a = [1,2,3]
b = [2,3,4]
c= [a,b]
itertools.product(*c)
You can pass array of arguments using *

Categories