+ 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
3 odpowiedzi
+ 5
print(f[(8 % len(f))-1])
+ 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]) # []
+ 1
SAYED🇧🇩🇵🇸 lol you are legend!
Thanks!