0

Hello I Have This Code

loss = list(range(1,10))

lists_fru = ['apple','banana','strawberry','erdberry','mango']

for index ,i in enumerate(loss):

    if i > len(lists_fru):
        print('larg')
        
    else:
        print(lists_fru[index])

The Resul Of It

apple                                                                                                                                                                                
banana                                                                                                                                                                               
strawberry                                                                                                                                                                           
erdberry                                                                                                                                                                             
mango                                                                                                                                                                                
larg                                                                                                                                                                                 
larg                                                                                                                                                                                 
larg                                                                                                                                                                                 
larg  

What I'm Looking For Or What I'm Trying To Do

I Wanna when the list_fru end to complete the loop from the begining

Like This

apple                                                                                                                                                                                
banana                                                                                                                                                                               
strawberry                                                                                                                                                                           
erdberry                                                                                                                                                                             
mango                                                                                                                                                                                
apple                                                                                                                                                                                 
banana                                                                                                                                                                                 
strawberry                                                                                                                                                                                 
erdberry

like this

1 Answer 1

2

You can do what you want using the modulo operator, %.

loss = list(range(1,10))

lists_fru = ['apple','banana','strawberry','erdberry','mango']

for index ,i in enumerate(loss):
   print(lists_fru[index % len(lists_fru)])
Sign up to request clarification or add additional context in comments.

3 Comments

i think in this method enumerate is useless.
@GiorgiImerlishvili You are probably right, but if you don't use enumerate the output doesn't match what's expected/wanted.
I think OP only used enumerate because he couldn't think of a better way to tackle this problem. In your solution it is not needed anymore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.