-1

I'm new on Python,

Code

bright_fruit = ['banana','orange','mango']
red_fruit = ['tomato','grape','apple']


for x in bright_fruit:
  for y in red_fruit:
    print(x, y)

and the result like this


>banana tomato
>banana grape
>banana apple
>orange tomato
>orange grape
>orange apple
>mango tomato
>mango grape
>mango apple

what i need is print pair from array like this

>banana tomato
>orange grape
>mango apple

I will use this code to more specific cases, but can anyone fix this?

thanks for help.

Uchsun
  • 361
  • 1
  • 8
  • 25

2 Answers2

1

Use the zip function:

bright_fruit = ['banana','orange','mango']
red_fruit = ['tomato','grape','apple']
for x, y in zip(bright_fruit, red_fruit):
    print(x,y)

Alternatively you could use the index value in order to print both:

for i in range(len(bright_fruit)):
    print(bright_fruit[i], red_fruit[i])
Alexander
  • 16,091
  • 5
  • 13
  • 29
-1

Try this:

bright_fruit = ['banana','orange','mango']
red_fruit = ['tomato','grape','apple']

for fruit1, fruit2 in zip(bright_fruit, red_fruit):
    print(fruit1, fruit2)
Martin
  • 1
  • 1