enlorans
enlorans

Reputation: 9

Python For Loop enumerate control

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

Upvotes: 0

Views: 26

Answers (1)

norie
norie

Reputation: 9857

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)])

Upvotes: 2

Related Questions