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

青灯夜游
Release: 2022-07-11 20:21:12
forward
2762 people have browsed it

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
Copy after login
import numpy as np
print(np.arange(3,6)) # [3 4 5]

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

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
Copy after login

【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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template