Home > Back-end >  Print rows with certain values from a matrix
Print rows with certain values from a matrix

Time:01-30

I have a matrix with a bike in each row. I would like to print all the rows containing a certain bike, f.ex "Trek". I am struggeling to find the correct code. The code below gives the following error message AttributeError: 'list' object has no attribute 'values'. Anybody have any ideas on how to solve this?

bikes = [["Giant", "Steel", "58 cm", "1000"], ["Trek", "Aluminium", "56 cm", "6000"], 
    ["Trek", "Carbon", "60 cm", "8000"], ["Specialized", "Carbon", "60 cm", "10000"],
    ["Trek", "Aluminium", "58 cm", "1500"]]


print(bikes[bikes.values == "Trek"])

CodePudding user response:

Try this:

print([bike for bike in bikes if bike[0] == "Trek"])
  •  Tags:  
  • Related