Search code examples
pythonreplacenested-lists

How to get element within range for nested list


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

  • You can use this code. Try to iterate over the list with condition including low and high

    tlist = [['Josh', 'Yes', 1, 4], ['Amy', 'No', 30, 4], ['Zen', 'No', 90, 1]]
    low = int(input("Enter low")) #take this as 1
    high = int(input("Enter High")) #take this as 50
    tlist = [l for l in tlist if l[2]>=low and l[2]<=high]
    print(tlist)