Q:

Given a two Python list. Write a python program to iterate both lists simultaneously and display items from list1 in original order and items from list2 in reverse order.

belongs to collection: Python List Exercises

0

Iterate both lists simultaneously

Given a two Python list. Write a program to iterate both lists simultaneously and display items from list1 in original order and items from list2 in reverse order.

Given

list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]

Expected output:

10 400
20 300
30 200
40 100

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.

Solution:

  • The zip() function can take two or more lists, aggregate them in a tuple, and returns it.
  • Pass the first argument as a list1 and seconds argument as a list2[::-1] (reverse list using list slicing)
  • Iterate the result using a for loop
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]

for x, y in zip(list1, list2[::-1]):
    print(x, y)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Remove empty strings from the list of strings usin... >>
<< Concatenate two lists in the following order using...