Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading