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

Code mostly works except the second answer problem with quotes in list

    def car_makes(all_cars: str) -> list:
    lst = all_cars.split(",")
        car_brands = []
        for i in lst:
            brand = i.split(" ")
            car_brands.append(brand[0])
            firms = list(dict.fromkeys(car_brands))
        return firms

    if __name__ == '__main__':
    print(car_makes("Audi A4,Skoda Super,Skoda Octavia,BMW 530,Seat Leon,Skoda Superb,Skoda Superb,BMW x5"))
    print(car_makes(""))

 
answers

    ['Audi', 'Skoda', 'BMW', 'Seat']
    []

Code mostly works except the second answer has to stay empty but I get [”]. What am I missing here?

>Solution :

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

From the str.split documentation:

Splitting an empty string with a specified separator returns [''].

You could check whether the given string is empty and return an empty list in that case:

def car_makes(all_cars: str) -> list:
    if all_cars == "":
        return []
    lst = all_cars.split(",")
    # ...
    return firms
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