-1

i'm new to python and i'm still learning it. i really need your help on how to print both index of two list using nested loop in python. for example i want to print both index 0 to index 2 of my 2 list.

Example:

List1 = [Ryan, John, Steve, Robert]
List2 = [Anna, Ruby, Ella]

Output should be like this:

Ryan loves Anna
John loves Ruby
Steve loves Ella

then it will ignore the value of 3rd index of List1.

Faded
  • 31
  • 1
  • 9
  • 3
    Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – DjaouadNM Dec 21 '19 at 09:58
  • no, it's not what the output i want. and i'm using python 3. – Faded Dec 21 '19 at 12:42

1 Answers1

0

Zip hanldes list of different sizes just fine:

list1 = ['Ryan', 'John', 'Steve', 'Robert']
list2 = ['Anna', 'Ruby', 'Ella']

for a, b in zip(list1, list2):
    print(f'{a} loves {b}')

Output:

Ryan loves Anna
John loves Ruby
Steve loves Ella
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
9mat
  • 1,194
  • 9
  • 13
  • thanks sir, it works. but how about if i want a output of `Ryan loves Anna John loves Ruby Steve loves Ella Robert is single` how can i do it with zip? – Faded Dec 21 '19 at 12:40
  • to print it in one line you need join: `print(' '.join([f'{a} loves {b}' for a,b in zip(list1, list2)])` – 9mat Dec 22 '19 at 00:49