Questions tagged [argument-unpacking]

Use this tag for questions related with argument unpacking, a technique that allows Arrays and Traversable objects to be extracted/unpacked into argument lists/sequences.

is usually found in PHP, Python and Go.

144 questions
1
vote
2 answers

python tuples: unpacking into a list using *args

Hello and thanks for your time; I am using *args to take various arguments, which creates a tuple. How do I 'unpack' a tuple into a list? The following code appends the list with a tuple. grades = [] def add_grades(*args): …
JulienEast
  • 27
  • 7
1
vote
0 answers

Space in unpacked keywords does not lead to syntax error

When handling connection string attributes, I noticed a curious behavior in Python's argument unpacking. Passing in an argument that has spaces in the name works. def f(**kwargs): print(kwargs) f(spaced keyword=3) # Syntax…
Felix
  • 2,548
  • 19
  • 48
1
vote
1 answer

Python- Unpack A List in Pandas Slicer

I wrote a function that does something similar like following. The function expects a multi-level dataframe. column_name argument can be ['Deal Name','Deal Expense']. To filter a multi-level dataframe, I would need to unpack the column_name list…
1
vote
1 answer

Iteratively unpack function output - error: too many values to unpack

I wrote a function which returns different outputs and I need to iteratively call the function for each element of an array (passed as an iterable). However, I am only able to retrieve the output as a tuple and I can't figure out how to unpack each…
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
1
vote
0 answers

Does unpacking sequences [strings, lists] and sets involve casting into a tuple?

I just learnt of unpacking with the * and so went on some test runs. I realized that although some write about passing in tuples as arguments in place of positional arguments, I could also pass in strings, lists and sets and Python wouldn't complain…
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
1
vote
1 answer

Iterating through elements of variable arguments

I have a function scalar_func(*args) that takes a variable number scalar numbers. It preforms some math with them and outputs a scalar. As a trivial example, we'll assume scalar_func multiplies each number: def scalar_func(*args): out = 1 …
1
vote
1 answer

Unpack tuple to member initialization or superclass constructor

Is it possible to initialize a member of a class (or call superclass constructor) by using the arguments contained in a tuple? Please note that I am aware of std::make_from_tuple() and std::apply() but they cannot be used in this scenario. Consider…
1
vote
1 answer

named tuple from dictionary using double-star-operator: are nested fields unpacked too?

I have two classes: Top and Nested, and to create both of them I need to provide TopDefinition and NestedDefinition objects, which are of type NamedTuple (definitions are required for type annotations). And Class Top contains attribute, which is a…
John Overiron
  • 517
  • 1
  • 5
  • 20
1
vote
1 answer

how to remove padding in struct python during unpacking

When I use packing in struct, I find out that during unpacking that I have extra characters after I unpack the byte object. For example before packing: c = b'CONNECT' value gotten after unpacking using struct is b'CONNECT\x00\x00\x00\x00\x00' Here…
Francis
  • 61
  • 1
  • 4
1
vote
2 answers

How to remove/ignore unexpected keyword arguments when passing as dictionary?

The following code def f(par1, par2): print("par1 = %s, par2 = %s" % (str(par1), str(par2))) pars = { 'par1': 12, 'par2': 13, 'par3': 14 } f(**pars) raises error TypeError: f() got an unexpected keyword argument 'par3' How to…
Dims
  • 47,675
  • 117
  • 331
  • 600
1
vote
2 answers

How to unpack a list of lists?

This may be a duplicate question, but I want to find out is there a way to unpack a list of lists and make a variable from an unpacked result? I have a data in the file like: '[416213688, 422393399, 190690902, 81688],| [94925847, 61605626,…
NagaevAF
  • 21
  • 1
  • 4
1
vote
0 answers

Strange unpacking of a custom dictionary / mapping in Jython

I have run into a strange dictionary / mapping unpacking behavior in Jython. Originally, in the context of SQLAlchemy, but I managed to narrow it down to the following minimal example: import collections class…
Leonid Shifrin
  • 22,449
  • 4
  • 68
  • 100
1
vote
3 answers

Unpacking argument in print statements

I would like to know if there is a way to unpack values in a print statement, the same way it is possible to unpack arguments in a function f This works: import numpy er = numpy.array([0.36666667, 0.46666667, 0.43333333, numpy.nan]) l =…
Nuageux
  • 1,668
  • 1
  • 17
  • 29
1
vote
2 answers

TypeError: cannot do slice indexing on with these indexers [(2,)] of

I've a user defined function as follows:- def genre(option,option_type,*limit): option_based = rank_data.loc[rank_data[option] == option_type] top_option_based = option_based[:limit] print(top_option_based) …
Nayana Madhu
  • 1,185
  • 5
  • 17
  • 34
1
vote
2 answers

Get first item of returning function value

I'm working with kinterbasdb module, and the module has this function called fetchone, that returns the result of an execution of a query: cursor.execute("....") cursor.fetchone() This returns a tuple: ("value1",), and i want access the first item,…
user7427352