+ 2

how to iterate over list even if length of list ends ?

f=['f', 'l', 'a', 'm', 'e', 's' ] length=8 length is inputed by user it can be anything... now we have to iterate over list using length like this : F L A M E S 1 2 3 4 5 6 7 8 here length ends so remover 'L' from the list F A M E S 1 2 3 4 5 6 7 8 again now remove the 'M' from the list F A E S 1 2 3 4 5 6 7 8 remove 'S' F A E 1 2 3 4 5 6 7 8 remove 'A' F E 1 2 3 4 5 6 7 8 Remove 'E' now only one letter left now we want this remaining letter as output... hope you got my idea.. I tried to do but by spending 5 hours still I am stucked and my code is not working.... for diffrent diffrent inputs : https://code.sololearn.com/cRt8sCDjp8kk/?ref=app

18th May 2021, 4:25 AM
Ratnapal Shende
Ratnapal Shende - avatar
3 Respuestas
+ 5
print(f[(8 % len(f))-1])
18th May 2021, 4:41 AM
TOLUENE
TOLUENE - avatar
+ 4
Ratnapal Shende , there is a nice behavior using a slice on a list (or other iterables). it does not matter if the start or end index is available in the sequence, no error will be thrown (it is handled internally with a silent exception) lst = ['l','a','m'] print(lst[1:5]) # ['a', 'm'] print(lst[2:5]) # ['m'] print(lst[3:5]) # []
19th May 2021, 12:32 PM
Lothar
Lothar - avatar
+ 1
SAYED🇧🇩🇵🇸 lol you are legend! Thanks!
18th May 2021, 5:04 AM
Ratnapal Shende
Ratnapal Shende - avatar