accessing each element of a list according to given list of indexes TypeError

Viewed 24

I want to access each element of the area according to given list of indexes for loop, but I am getting a TypeError: 'float' object is not subscriptable.

Is there a way I can do that ?

really appreciated

index=[0, 1, 5, 6, 10, 11]
area=[78.0, 125.0, 203.0, 266.0, 344.0, 141.0, 46.0, 187.0, 245.0, 265.0, 78.0, 203.0]
key_to_del = False

for x in index:    
    for j in area:
        if float(j[x]) >150:
            print(j[x])
           
1 Answers

You don't need to loop through area with j, because you are looping through the index and calling certain values of area based on index, so you can just use:

index=[0, 1, 5, 6, 10, 11]
area=[78.0, 125.0, 203.0, 266.0, 344.0, 141.0, 46.0, 187.0, 245.0, 265.0, 78.0, 203.0]
key_to_del = False

for x in index:
    if area[x] >150:
        print(area[x])

203.0

Out of the specified values in the index, the final index value 11 is the only one that returns a number above 150, which is 203 which is index of 11 or position 12.

Related