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"
>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)