Issue
I have 2 lists. I only want to append to the new list after I've reached the specified value.
old_list = ['apple', 'pear','orange', 'banana', 'grape']
new_list = []
value = 'orange'
The desired outcome is:
new_list = ['banana', 'grape']
I've tried this, but the result is not what I want:
for i in old_list:
if i != value:
new_list.append(i)
Hope that makes sense!
Solution
Use the list.index
method to return the index i
where the value
appears in the old_list
. Then slice old_list
from index i+1
up to its end:
old_list = ['apple', 'pear','orange', 'banana', 'grape']
value = 'orange'
i = old_list.index(value)
new_list = old_list[i+1:]
Answered By - jfaccioni Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.