Home >Backend Development >Python Tutorial >Let's talk about adding an asterisk (**) before lists and dictionaries in Python

Let's talk about adding an asterisk (**) before lists and dictionaries in Python

青灯夜游
青灯夜游forward
2022-07-11 20:21:122754browse

Why are there asterisks (**) in front of Python lists and dictionaries? The following article will talk to you about the reasons for adding an asterisk (**) before lists and dictionaries in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Let's talk about adding an asterisk (**) before lists and dictionaries in Python

In Python, the single asterisk * and the double asterisk ** are except for "multiplication" and "power" values. In addition to operators, it also plays an important role in the operations of lists, tuples, and dictionaries.

1. Add an asterisk in front of the list and tuple *

The effect of adding an asterisk in front of the list is to Unpack (unpacke) into multiple independent parameters and pass them into the function.

def add(a, b):
    return a + b

data = [7, 8]
print(add(*data)) # 15
import numpy as np
print(np.arange(3,6)) # [3 4 5]

list2 = [3, 6]
print(np.arange(*list2)) # [3 4 5]

2. Add two asterisks in front of the dictionary (dict) **

Add two asterisks in front of the dictionary to interpret the dictionary Open as an independent element as a formal parameter.

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def add(a, b):
    return a + b

data = {'a':7, 'b':8}
print(add(**data)) # 15

【Related recommendations: Python3 video tutorial

The above is the detailed content of Let's talk about adding an asterisk (**) before lists and dictionaries in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete