
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to iterate over multiple lists simultaneously?
Here we use .zip() for iterative over multiple lists simultaneously.zip() takes n number of iterables and returns list of tuples. i-th element of the tuple is created using the ith element from each of the iterables.
Example
L1=[1,2,3,4] L2=[‘aa’,’bb’,’cc’,’dd’] L=zip(L1,L2) Output [(1,’aa’),(2,’bb’),(3,’cc’),(4,’dd’)]
Algorithm
Step 1: first create 3 user input list. Step 2 : use .zip() function. Step 3: print tuples.
Example code
# To iterate over 3 lists using zip function importitertools A=list() B=list() C=list() n = int(input("How many you data want to store??")) print("Enter Roll no.") for i in range(int(n)): k=int(input("")) A.append(k) print("Enter Name ::") for j in range(int(n)): k1=input("") B.append(k1) print("Enter Age ::") for j in range(int(n)): k1=int(input("")) C.append(k1) print ("\niterating using zip") for (a, b, c) in zip(A, B, C): print (a, b, c)
Output
How many you data want to store??4 Enter Roll no. 56 89 67 34 Enter Name :: yui jkl vm dfg Enter Age :: 7 9 6 5 iterating using zip 56 yui 7 89 jkl 9 67 vm 6 34 dfg 5
- Related Articles
- Iterate over lines from multiple input streams in Python
- Java Program to Iterate over enum
- Haskell Program to Iterate over enum
- Java Program to Iterate over an ArrayList
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- C++ Program to Iterate Over a Dictionary
- C++ Program to Iterate Over an Array
- Swift Program to Iterate Over an Array
- Golang program to iterate over a Slice
- Python - Ways to iterate tuple list of lists
- Iterate over a dictionary in Python
- Iterate over a list in Python
- Iterate over a set in Python
- Java Program to Iterate over ArrayList using Lambda Expression

Advertisements