Just started learning python, I have this assignment where I need do range within nested list.
tlist = [['Josh', 'Yes', 1, 4], ['Amy', 'No', 30, 4], ['Zen', 'No', 90, 1]]
I want to ask for the low and higher value for tlist[2]
low = int(input("Enter low")) #take this as 1
high = int(input("Enter High")) #take this as 50
this need to be updated and when I call the list again it should show, and remove ‘zen’ from the list
tlist = [['Josh', 'Yes', 1, 4], ['Amy', 'No', 30, 4]]
>Solution :
Loop through each inner list in t_list and access the the third element (index 2). Check if this element is not found in the range [low,high]. If not found, remove current inner list from t_list.
tlist = [['Josh', 'Yes', 1, 4], ['Amy', 'No', 30, 4], ['Zen', 'No', 90, 1]]
low = 1
high = 50
for inner_list in tlist:
if inner_list[2] < low or inner_list[2] > high:
tlist.remove(inner_list)
print(tlist)
Ouput :
[['Josh', 'Yes', 1, 4], ['Amy', 'No', 30, 4]]