Summary of knowledge points of zip() function in Python3

1 Introduction

In this article, I will give you an in-depth understanding Pythonof the functions in zip(), and using them can improve your work efficiency.
Without further ado, let's get started!

2. Basic knowledge

First, let's introduce some basic knowledge points:

  • Some data types in Python are immutable (e.g. strings, integers), while others are mutable (e.g. lists and dictionaries). Immutable data objects cannot be changed after creation, mutable objects can.
  • An iterable is an object that returns each of its member elements individually. For example, lists, tuples, strings, and dictionaries are iterable objects. We can use iter()or forloop to iterate over an iterable.
  • When an object returns an iterator, we must use it to retrieve an object that we can see or use.

3. Pass parameters to the zip function

We can zip()pass any number of iterables in the function:

3.1 Passing zero parameters

A sample is as follows:

>>> zipped = zip()
>>> list(zipped)
[]

In the above code, we zip()passed zero elements to the function, at which point the function returns empty.

3.2 Passing a parameter

Passing an argument creates a collection of tuples with one element in each tuple.

The sample code is as follows:

# create a list of student names
>>> student_names = ['Lindsay', 'Harry', 'Peter']
# zip the list 
>>> zipped  = zip(student_names)
# consume with list()
>>> list(zipped)
[('Lindsay',), ('Harry',), ('Peter',)]

In the above code, we have created a list with three strings representing the names of three students.

3.3 Passing two parameters

Passing two arguments will create a collection with pairs of tuples where the first element comes from the first argument and the second element comes from the second argument.

The sample code is as follows:

# create a list of student ids 
>>> student_ids = ['123', '4450', '5600']
# create a list of student names again, so that we do not forget the earlier steps!
>>> student_names = ['Lindsay', 'Harry', 'Peter']
# zip the lists 
>>> zipped  = zip(student_names, student_ids)
>>> list(zipped)
[('Lindsay', '123'), ('Harry', '4450'), ('Peter', '5600')]

In the above code, we create another list containing three strings. At this time, each element is used to represent student_namesthe correspondence of each student student_ids.

At this point, we can use the for loop to traverse the access, the sample code is as follows:

>>> student_names = ['Lindsay', 'Harry', 'Peter']
>>> student_ids = ['123', '4450', '5600']
>>> for student_name, student_id in zip(student_names, student_ids): 
...     print(student_name, student_id)
... 
Lindsay 123
Harry 4450
Peter 5600

3.4 Passing parameters of different lengths

So far we've only looked at examples where each iterable has the same length: the lists containing student names and ids are both of length 3, but we can also pass iterables of different lengths. At this point, the zip function will return a collection of tuples with the number of tuples equal to the smallest-length iterable item. It ignores the rest of the elements in iterables with longer lengths, like so:

# student_ids is a list with 4 elements 
>>> student_ids = ['123', '4450', '5600', '1']
# student_namdes is a list with 3 elements 
>>> student_names = ['Lindsay', 'Harry', 'Peter']
# zip is completely ignoring the last element of student_ids 
>>> list(zip(student_names, student_ids))
[('Lindsay', '123'), ('Harry', '4450'), ('Peter', '5600')]

>>> for student_name, student_id in zip(student_names, student_ids): 
...     print(student_name, student_id)
... 
Lindsay 123
Harry 4450
Peter 5600

As you can see from the example above, the function does nothing with the last element in the pair zip. Therefore, it is very important to check the length of the iterable before passing to it .student_ids1zip()

4. Summary

This article focuses on the summary of basic knowledge points about the zip function in Python, and gives corresponding code examples.

Are you useless?

Guess you like

Origin blog.csdn.net/sgzqc/article/details/129229033