Home > database >  Print Whole List With Common Element
Print Whole List With Common Element

Time:04-22

I am trying to print the whole list (not just one item) if it includes Sydney and I am unsure what code I need to complete this.

car1 = ["Sydney", "CM11CAR", "Ford", "Fiesta", "Red", "3000", "1500", "3"]
car2 = ["Auckland", "CM22CAR", "Vauxhall", "Corsa", "Grey", "3100", "1550", "5"]
car3 = ["Brisbane", "CM33CAR", "Ford", "Focus", "Blue", "3100", "1550", "5"]

van1 = ["Brisbane", "CM11VAN", "Renault", "Master", "Green", "2900", "1450", "1500kg"]
van2 = ["Auckland", "CM22VAN", "Vauxhall", "Vivaro", "Blue", "3000", "1500", "2000kg"]
van3 = ["Perth", "CM33VAN", "Renault", "Traifc", "Red", "3100", "1550", "1500kg"]

minibus1 = ["Sydney", "CM11BUS", "Volkswagen", "Transporter", "Green", "3000", "1500", "5"]
minibus2 = ["Auckland", "CM22BUS", "Mercedes-Benz", "Sprinter", "Grey", "3100", "1550", "7"]
minibus3 = ["Perth", "CM33BUS", "Volkswagen", "California", "Blue", "3100", "1550", "5"]

CodePudding user response:

  1. First, you need to add an array holding all the other lists
  2. Use the following code
car1 = ["Sydney", "CM11CAR", "Ford", "Fiesta", "Red", "3000", "1500", "3"]
car2 = ["Auckland", "CM22CAR", "Vauxhall", "Corsa", "Grey", "3100", "1550", "5"]
car3 = ["Brisbane", "CM33CAR", "Ford", "Focus", "Blue", "3100", "1550", "5"]
lst = [car1, car2, car3]

for i in lst:
    for city in i:
        if(city == "Sydney") :
            for x in range(len(i)):
                    print(i[x])

CodePudding user response:

Put all your lists into one list. Like this:

vehicles = [car1, car2, car3, van1, van2, van3, minibus1, minibus2, minibus3]

Then loop over all the lists in vehicles and check the first element of that list which is a city and print the output. Like this:

for vehicle in vehicles:
    if vehicle[0] == 'Sydney':
        print(vehicle)
  • Related