Python:zip () function

Source: Internet
Author: User

Definition of the zip () function

    • Combine elements from multiple iterators in a parameter into a new iterator;
    • return: returns a Zip object whose inner elements are tuples, which can be converted to lists or tuples;
    • Incoming parameters: tuples, lists, dictionaries, and other iterators.

Use of the zip () function

When there is only one parameter in the ZIP () function

Zip (iterable) takes a meta-ancestor from the iterable in turn, forming a meta-ancestor

#Zip () function single parameterList1 = [1, 2, 3, 4]tuple1=Zip (list1)#Print the return type of a zip functionPrint("The return type of the zip () function: \ n", type (tuple1))#Convert a Zip object to a listPrint("the Zip object is converted to a list: \ n", List (tuple1))#Output ResultsThe return type of the zip () function:<class 'Zip'>the Zip object is converted to a list: [(1,), (2,), (3,), (4,)]

When there are multiple parameters in the ZIP () function:

#Practice AreaList_1 = ['a','b','C','D','e','F','g','h']list_2= [9, 8, 7, 6, 5, 4, 3, 2 ] forIinchRange (len (list_1)):ifList_2[i]% 2 = = 1:        Print(list_1[i],end=' ') Print("")#in this case, we'll use a zip to package two lists into a list of two ancestors. forI,jinchZip (list_1,list_2):ifJ% 2 = =0:Print(i,end=' ') A c e G b d F h

Python:zip () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.