argument-unpacking

Python : * Operator in list comprehension

Python : * Operator in list comprehension Question: Evening all, I have recently discover the * operator to unpack my list. I find it quite elegant but I am a bit struggling with it. Please find below an example : from matplotlib.pyplot import Line2D COLOR_FCT = { "a": ["blue", "Al", "-"], "b": ["orange", "Bv", "-"], …

Total answers: 1

Why do I need '**' when loading json (Python)

Why do I need '**' when loading json (Python) Question: t1 = Tournament("Aeroflot Open", 2010) json_data = json.dumps(t1.__dict__) print(json_data) t = Tournament(**json.loads(json_data)) # <——————- print(f"name = {t.name}, year = {t.year}") Could someone explain me why do I need these two asterisks ** when loading the json back. The line is above. Asked By: David || …

Total answers: 1

Invalid unpacking arguments

Invalid unpacking arguments Question: I was reading an online document explaining unpacking (*args and **kwargs). Getting confused by the following two asserts, not sure why the second function is invalid. Could anyone help me to understand the reason? def f(x, y, z): return [x, y, z] t = (3,) d = {"z": 4} assert f(2, …

Total answers: 1

Python keyword arguments unpack and return dictionary

Python keyword arguments unpack and return dictionary Question: I have a function definition as below and I am passing keyword arguments. How do I get to return a dictionary with the same name as the keyword arguments? Manually I can do: def generate_student_dict(first_name=None, last_name=None , birthday=None, gender =None): return { ‘first_name’: first_name, ‘last_name’: last_name, ‘birthday’: …

Total answers: 6

Python: Splat/unpack operator * in python cannot be used in an expression?

Python: Splat/unpack operator * in python cannot be used in an expression? Question: Does anybody know the reasoning as to why the unary (*) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File “<stdin>”, …

Total answers: 3

Unpack NumPy array by column

Unpack NumPy array by column Question: If I have a NumPy array, for example 5×3, is there a way to unpack it column by column all at once to pass to a function rather than like this: my_func(arr[:, 0], arr[:, 1], arr[:, 2])? Kind of like *args for list unpacking but by column. Asked By: …

Total answers: 3

Python 3: starred expression to unpack a list

Python 3: starred expression to unpack a list Question: Example use: def f(a, b, c, d): print(a, b, c, d, sep = ‘&’) f(1,2,3,4) >>> 1&2&3&4 f(*[1, 2, 3, 4]) >>> 1&2&3&4 Where in the python documentation is * explained? Asked By: Kifsif || Source Answers: The *args calling convention is documented in the Expressions …

Total answers: 2

Class that acts as mapping for **unpacking

Class that acts as mapping for **unpacking Question: Without subclassing dict, what would a class need to be considered a mapping so that it can be passed to a method with **. from abc import ABCMeta class uobj: __metaclass__ = ABCMeta uobj.register(dict) def f(**k): return k o = uobj() f(**o) # outputs: f() argument after …

Total answers: 4

How to extract parameters from a list and pass them to a function call

How to extract parameters from a list and pass them to a function call Question: What is a good, brief way to extract items from a list and pass them as parameters to a function call, such as in the example below? Example: def add(a,b,c,d,e): print(a,b,c,d,e) x=(1,2,3,4,5) add(magic_function(x)) Asked By: falek.marcin || Source Answers: I …

Total answers: 3

Unpacking, extended unpacking and nested extended unpacking

Unpacking, extended unpacking and nested extended unpacking Question: Consider the following expressions. Note that some expressions are repeated to present the "context". (this is a long list) a, b = 1, 2 # simple sequence assignment a, b = [‘green’, ‘blue’] # list asqignment a, b = ‘XY’ # string assignment a, b = range(1,5,2) …

Total answers: 4