Hi Everyone,
I am trying to create a sublist which uses a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5).
I am able to acheive that using the below code but getting Index Error: List index out of range.
Thanks,
Shikha
I am trying to create a sublist which uses a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5).
I am able to acheive that using the below code but getting Index Error: List index out of range.
def sublist(l): my_list= [] idx= 0 while l[idx]!= 5: my_list.append(l[idx]) idx= idx+1 return my_list p= [3,4,2,43,5,6] print(sublist(p))I appreciate any help on this.
Thanks,
Shikha