Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does *tuple and **dict mean in Python? [duplicate]

As mentioned in PythonCookbook, * can be added before a tuple. What does * mean here?

Chapter 1.18. Mapping Names to Sequence Elements:

from collections import namedtuple Stock = namedtuple('Stock', ['name', 'shares', 'price']) s = Stock(*rec)  # here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45) 

In the same section, **dict presents:

from collections import namedtuple Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time']) # Create a prototype instance stock_prototype = Stock('', 0, 0.0, None, None) # Function to convert a dictionary to a Stock def dict_to_stock(s):     return stock_prototype._replace(**s) 

What is **'s function here?

like image 317
heLomaN Avatar asked Feb 16 '14 08:02

heLomaN


People also ask

What does * tuple mean in Python?

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

Does tuple allow duplicates in Python?

31.2 Python Collection Types Tuples A Tuple represents a collection of objects that are ordered and immutable (cannot be modified). Tuples allow duplicate members and are indexed.

What does * mean in Python function?

The asterisk (star) operator is used in Python with more than one meaning attached to it. For numeric data types, * is used as multiplication operator >>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j.

What is tuple and dictionary in Python?

A dictionary is a hash table of key-value pairs. List and tuple is an ordered collection of items. Dictionary is unordered collection. List and dictionary objects are mutable i.e. it is possible to add new item or delete and item from it. Tuple is an immutable object.


1 Answers

In a function call

*t means "treat the elements of this iterable as positional arguments to this function call."

def foo(x, y):     print(x, y)  >>> t = (1, 2) >>> foo(*t) 1 2 

Since v3.5, you can also do this in a list/tuple/set literals:

>>> [1, *(2, 3), 4] [1, 2, 3, 4] 

**d means "treat the key-value pairs in the dictionary as additional named arguments to this function call."

def foo(x, y):     print(x, y)  >>> d = {'x':1, 'y':2} >>> foo(**d) 1 2 

Since v3.5, you can also do this in a dictionary literals:

>>> d = {'a': 1} >>> {'b': 2, **d} {'b': 2, 'a': 1} 

In a function signature

*t means "take all additional positional arguments to this function and pack them into this parameter as a tuple."

def foo(*t):     print(t)  >>> foo(1, 2) (1, 2) 

**d means "take all additional named arguments to this function and insert them into this parameter as dictionary entries."

def foo(**d):     print(d)  >>> foo(x=1, y=2) {'y': 2, 'x': 1} 

In assignments and for loops

*x means "consume additional elements in the right hand side", but it doesn't have to be the last item. Note that x will always be a list:

>>> x, *xs = (1, 2, 3, 4) >>> x 1 >>> xs [2, 3, 4]  >>> *xs, x = (1, 2, 3, 4) >>> xs [1, 2, 3] >>> x 4  >>> x, *xs, y = (1, 2, 3, 4) >>> x 1 >>> xs [2, 3] >>> y 4  >>> for (x, *y, z) in [ (1, 2, 3, 4) ]: print(x, y, z) ... 1 [2, 3] 4 

Note that parameters that appear after a * are keyword-only:

def f(a, *, b): ...  f(1, b=2)  # fine f(1, 2)    # error: b is keyword-only 

Python3.8 added positional-only parameters, meaning parameters that cannot be used as keyword arguments. They appear before a / (a pun on * preceding keyword-only args).

def f(a, /, p, *, k): ...  f(  1,   2, k=3)  # fine f(  1, p=2, k=3)  # fine f(a=1, p=2, k=3)  # error: a is positional-only 
like image 85
Elazar Avatar answered Sep 23 '22 12:09

Elazar