{"id":7322,"date":"2021-09-30T12:00:02","date_gmt":"2021-09-30T06:30:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=7322"},"modified":"2021-11-22T18:35:27","modified_gmt":"2021-11-22T13:05:27","slug":"python-program-to-iterate-through-two-lists-in-parallel","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-iterate-through-two-lists-in-parallel\/","title":{"rendered":"Python Program to Iterate Through Two Lists in Parallel"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n List:<\/strong><\/p>\n A list in Python is a collection of zero or more elements. A list element can be any type of data. It can be a string, a number, or a mixture of the two. In Python, a list is equivalent to an array in C or Java. Lists are mutable, which means you may change their content, and they contain a variety of helpful specialized methods.<\/p>\n When working with Python lists, we may encounter an issue in which we must iterate over two list entries. Iterating one after the other is an option, but it is more time consuming, and a one-two liner is always preferred. Let’s go over several different approaches to doing this assignment.<\/p>\n Given two lists, the task is to iterate two lists simultaneously.<\/p>\n Examples:<\/strong><\/p>\n Printing list one after other:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Printing list in parallel:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to iterate through two lists in parallel in python:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n The combination of the above functionalities may make our job easier. The disadvantage here is that we may have to concatenate the list, which would require more RAM than desired.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This approach is identical to the one before, but it is somewhat more memory efficient because chain() is used to execute the work and internally builds an iterator.<\/p>\n Chain function will be available in itertools.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Zip in Python 3 returns an iterator. When any of the lists in the zip() function is exhausted, the function terminates. In other words, it continues until the smallest of all the lists is reached. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When all lists have been exhausted, zip longest comes to a halt. When the shorter iterator(s) have been exhausted, zip longest returns a tuple with the value None. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. List: A list in Python is a collection of zero or more elements. A list element can be any type of data. It can be a string, a number, …<\/p>\nsamplelistone = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\nsamplelisttwo = [38, 23, 10, 20]<\/pre>\n
printing the samplelistone hello this is BTechGeeks Python\r\nprinting the samplelisttwo 38 23 10 20\r\nprinting the two lists elements simultaneously \r\nhello this is BTechGeeks Python 38 23 10 20<\/pre>\n
samplelistone = [27823, 9792, \"hello\", \"this\", True, 7.6]\r\nsamplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']<\/pre>\n
printing the samplelistone 27823 9792 hello this True 7.6\r\nprinting the samplelisttwo sky is blue 122 5.3214 False hello\r\nprinting the two lists elements simultaneously \r\n27823 9792 hello this True 7.6 sky is blue 122 5.3214 False hello<\/pre>\n
samplelistone = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\nsamplelisttwo = [38, 23, 10, 20, 31]<\/pre>\n
printing the samplelistone hello this is BTechGeeks Python\r\nprinting the samplelisttwo 38 23 10 20 31\r\nprinting the two lists elements simultaneously \r\nhello 38\r\nthis 23\r\nis 10\r\nBTechGeeks 20\r\nPython 31<\/pre>\n
samplelistone = [27823, 9792, \"hello\", \"this\", True, 7.6]\r\nsamplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']<\/pre>\n
printing the samplelistone 27823 9792 hello this True 7.6\r\nprinting the samplelisttwo sky is blue 122 5.3214 False hello\r\nprinting the two lists elements simultaneously \r\n27823 sky\r\n9792 is\r\nhello blue\r\nthis 122\r\nTrue 5.3214\r\n7.6 False<\/pre>\n
Program to Iterate Through Two Lists in Parallel in Python<\/h2>\n
\n
Method #1:Using for loop and “+” operator<\/h3>\n
# initializing lists\r\nsamplelistone = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\nsamplelisttwo = [38, 23, 10, 20]\r\n# printing the two original lists\r\nprint(\"printing the samplelistone \", *samplelistone)\r\nprint(\"printing the samplelisttwo \", *samplelisttwo)\r\n# traversing the two lists simulatenously\r\nprint(\"printing the two lists elements simultaneously \")\r\nfor value in samplelistone + samplelisttwo:\r\n print(value, end=\" \")\r\n<\/pre>\n
printing the samplelistone hello this is BTechGeeks Python\r\nprinting the samplelisttwo 38 23 10 20\r\nprinting the two lists elements simultaneously \r\nhello this is BTechGeeks Python 38 23 10 20<\/pre>\n
Method #2: Using chain() function<\/h3>\n
from itertools import chain\r\n# initializing lists\r\nsamplelistone = [27823, 9792, \"hello\", \"this\", True, 7.6]\r\nsamplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']\r\n# printing the two original lists\r\nprint(\"printing the samplelistone \", *samplelistone)\r\nprint(\"printing the samplelisttwo \", *samplelisttwo)\r\n# traversing the two lists simulatenously\r\nprint(\"printing the two lists elements simultaneously \")\r\nfor value in chain(samplelistone, samplelisttwo):\r\n print(value, end=\" \")\r\n<\/pre>\n
printing the samplelistone 27823 9792 hello this True 7.6\r\nprinting the samplelisttwo sky is blue 122 5.3214 False hello\r\nprinting the two lists elements simultaneously \r\n27823 9792 hello this True 7.6 sky is blue 122 5.3214 False hello<\/pre>\n
Method #3:Using zip()<\/h3>\n
\nThe zip method and itertools.izip are implemented below, and itertools.izip iterates over two lists:<\/p>\nimport itertools\r\n# initializing lists\r\nsamplelistone = [27823, 9792, \"hello\", \"this\", True, 7.6]\r\nsamplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']\r\n# printing the two original lists\r\nprint(\"printing the samplelistone \", *samplelistone)\r\nprint(\"printing the samplelisttwo \", *samplelisttwo)\r\n# traversing the two lists simulatenously\r\nprint(\"printing the two lists elements simultaneously \")\r\nfor i, j in zip(samplelistone, samplelisttwo):\r\n print(i, j)\r\n<\/pre>\n
printing the samplelistone 27823 9792 hello this True 7.6\r\nprinting the samplelisttwo sky is blue 122 5.3214 False hello\r\nprinting the two lists elements simultaneously \r\n27823 sky\r\n9792 is\r\nhello blue\r\nthis 122\r\nTrue 5.3214\r\n7.6 False<\/pre>\n
Method #4:Using itertools.zip_longest()<\/h3>\n
\nThe itertools implementation is shown below.
\nzip longest iterates across two lists<\/p>\nimport itertools\r\n# initializing lists\r\nsamplelistone = [27823, 9792, \"hello\", \"this\", True, 7.6]\r\nsamplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']\r\n# printing the two original lists\r\nprint(\"printing the samplelistone \", *samplelistone)\r\nprint(\"printing the samplelisttwo \", *samplelisttwo)\r\n# traversing the two lists simulatenously\r\nprint(\"printing the two lists elements simultaneously \")\r\nfor i, j in itertools.zip_longest(samplelistone, samplelisttwo):\r\n print(i, j)\r\n<\/pre>\n
printing the samplelistone 27823 9792 hello this True 7.6\r\nprinting the samplelisttwo sky is blue 122 5.3214 False hello\r\nprinting the two lists elements simultaneously \r\n27823 sky\r\n9792 is\r\nhello blue\r\nthis 122\r\nTrue 5.3214\r\n7.6 False\r\nNone hello<\/pre>\n
\n