Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize sequence unpacking in python

Shulou Source: shulou.com Published: 2022-06-01 05:18:54 11月03日 Update

This article is about how python unpacks sequences. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Sequence unpacking

We define a variable and store the first and last names of the first names. If we want to extract their first and last names separately and store them in different variables, our easiest way is to use the split () function:

Name = "Xiao Chen" str_list = name.split () first_name = str_list [0] last_name = str_list [1]

In fact, this code can be rewritten as:

Name = "Xiao Chen" first_name,last_name = name.split ()

We directly assign the elements in the list returned by the split () function to first_name and last_name. This operation is called sequence unpacking in python, where the sequence is not necessarily a list, it can be a tuple, or even a range.

Thank you for reading! This is the end of the article on "how to unpack the python sequence". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Sequence content function variable more article different good practical code element method name that is article look knowledge reference storage help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Microsoft vpn Apple