Home > Backend Development > Python Tutorial > How to pass list in Python function?

How to pass list in Python function?

WBOY
Release: 2023-05-09 19:04:37
forward
1553 people have browsed it

After passing the list to the function, the function can directly access the contents of the list.

Suppose there is a group of experts and we want to invite them to a seminar.

def send_invitation(experts):
    '''发送邀请函'''
    for expert in experts:
        print(expert + ',您好,现邀请您参加 XX 研讨会...')

experts = ['小明', '小红']
send_invitation(experts)
Copy after login

Run results:

Hello Xiao Ming, you are now invited to participate in the XX seminar...

Hello Huang Lili, you are now invited to participate in the XX seminar Will...

1 Modify the list

After the list parameter is passed to the function, the function can modify it.

Note: Any modifications made to the list in the function are permanent.

def send_invitation(experts, informed):
    '''发送邀请函,并移动列表数据到【已通知】列表'''
    while experts:
        expert = experts.pop()
        print(expert + ',您好,现邀请您参加 XX 研讨会...')
        informed.append(expert)

experts = ['小明', '小红']  # 专家列表
informed = []  # 已通知人员列表
print('执行前:experts=' + str(experts) + ',informed=' + str(informed))
send_invitation(experts, informed)
print('执行后:experts=' + str(experts) + ',informed=' + str(informed))
Copy after login

Run results:

Before execution: experts=['Xiao Ming', 'Xiao Hong'], informed=[]

Hello Xiaohong , you are now invited to participate in the XX seminar...

Hello Xiaoming, you are now invited to participate in the XX seminar...

After execution: experts=[],informed=[' Xiaohong', 'Xiaoming']

Even without comments, descriptive function names can clearly express the work done by the function.
We can also call another function within a function, which helps break down complex tasks into a series of steps and makes the program more readable.

2 Read-only list

Sometimes, we don’t want the function to modify the passed list,

In this case we can pass a copy of the list to the function:

experts = ['小明', '小红']  # 专家列表
informed = []  # 已通知人员列表
print('执行前:experts=' + str(experts) + ',informed=' + str(informed))
send_invitation(experts[:], informed)
print('执行后:experts=' + str(experts) + ',informed=' + str(informed))
Copy after login

Run result:

Before execution: experts=['Xiao Ming', 'Xiao Hong'], informed=[]

Xiaohong, hello, now inviting You are attending the XX seminar...

Hello Xiao Ming, you are now invited to attend the XX seminar...

After execution: experts=['Xiao Ming', 'Xiao Hong'] ,informed=['小红', '小明']

Although passing a copy of the list to the function can preserve the contents of the original list,

Unless there is a good reason to do so Do.

Because having the function use a passed list can improve performance by avoiding spending time

creating a copy in memory, which is especially important when dealing with large data lists.

The above is the detailed content of How to pass list in Python function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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