1. Introduction
In this article, I will walk you through an in-depth look at the zip() function in Python, the use of which can enhance your productivity.
Without further ado, let's get right to it!
2. Basics
First, let's cover some of the basics:
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 be changed.
An iterable object 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 a for loop to iterate over an iterable object.
When an object returns an iterator, we must use it to retrieve an object that we can see or use.
3. Passing parameters to the zip function
We can pass any number of iterable terms in the function zip():
3.1 Passing zero parameters
Sample example below:
>>> zipped = zip() >>> list(zipped) []
In the above code, we pass zero elements to the function zip(), which returns null at this point.
3.2 Passing a parameter
Passing a parameter creates a collection of tuples, each with an element in it.
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 of tuples with pairs, 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 have created another list containing three strings. At this point, each element is used to represent the corresponding student_ids for each student student_names.
At this point, we can use a 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 unequal length
So far, we've only looked at examples where each iterable is of the same length: the list containing the student's name and id are both of length 3, but we could also pass in iterables of different lengths. In this case, the zip function will return a collection of tuples, where the number of tuples is equal to the iterable with the smallest length. It will ignore the rest of the elements in the longer-length iterable item, as shown below:
# 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 zip does nothing with the last element 1 in student_ids. Therefore, it is important to check the length of the iterable items before passing them to zip().
4. Summary
This article focuses on Python on the zip function of the basic knowledge point summary, and gives the corresponding code examples.
This article on Python3 zip() function knowledge summary of the article is introduced to this, more related Python3 zip() function content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!