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 make list of cars into string?

Please help with this function.

def car_list_as_string(cars: list) -> str:
    """
    Create a list of cars.
    The order of the elements in the string is the same as in the list.

    [['Audi', ['A4']], ['Skoda', ['Superb']]] =>
    "Audi A4,Skoda Superb"
    """

I tried this but it’s unfinished…got stuck.

for car in cars:
    print(car)
    cars2 = ''.join(str(car) for car in cars)
    print(cars2)
    one_car = str(car[0])
    print(one_car)
    one_model = str(car[1])
    print(one_model)

Input is: [[‘Audi’, [‘A4’]], [‘Skoda’, [‘Superb’]]]
Output should be: "Audi A4,Skoda Superb"

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

>Solution :

If you want a code who also work for multiple nested models:

def car_list_as_string(cars: list):
    output_list: list[str] = []

    for brand, models in cars:
        for model in models:
            output_list.append(f"{brand} {model}")

    cars_string = ",".join(output_list)
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