How to make list of cars into string?
Question:
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"
Answers:
Try something like this, this is one approach to make your list of cars into string
def car_list_as_string(cars: list) -> str:
car_names = []
for car in cars:
car_names.append(f"{car[0]} {car[1][0]}")
return ", ".join(car_names)
- iterate over makes and models at once using a
for
loop
- construct the full names and append them to a
list
- join the list into a single string
def car_list_as_string(cars: list) -> str:
car_names = []
for make, models in cars:
car_names.append(' '.join([make, models[0]]))
return ", ".join(car_names)
Or to avoid indexing as Yevhen Kuzmovych suggested:
def car_list_as_string(cars: list) -> str:
car_names = []
for make, (model,) in cars:
car_names.append(' '.join([make, model]))
return ", ".join(car_names)
The (model,)
syntax means "unpack the 2nd element into a one element tuple".
For badly readable one-liners, this can work:
", ".join(f"{brand} {model}" for brand, models in dict(cars).items() for model in models)
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)
The way you do this depends on exactly what the output should look like if the input data are slightly different to that shown in the question.
The data look like this:
data = [['Audi', ['A4']], ['Skoda', ['Superb']]]
This is interesting because there’s no apparent reason for the model designation to be in a list. The implication is that the data might look something like this:
data = [['Audi', ['A4', 'Series II']], ['Skoda', ['Superb']]]
So let’s allow for that construct…
def process(_list):
return ','.join(' '.join([make] + model) for make, model in _list)
print(process(data))
Output:
Audi A4 Series II, Skoda Superb
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"
Try something like this, this is one approach to make your list of cars into string
def car_list_as_string(cars: list) -> str:
car_names = []
for car in cars:
car_names.append(f"{car[0]} {car[1][0]}")
return ", ".join(car_names)
- iterate over makes and models at once using a
for
loop - construct the full names and append them to a
list
- join the list into a single string
def car_list_as_string(cars: list) -> str:
car_names = []
for make, models in cars:
car_names.append(' '.join([make, models[0]]))
return ", ".join(car_names)
Or to avoid indexing as Yevhen Kuzmovych suggested:
def car_list_as_string(cars: list) -> str:
car_names = []
for make, (model,) in cars:
car_names.append(' '.join([make, model]))
return ", ".join(car_names)
The (model,)
syntax means "unpack the 2nd element into a one element tuple".
For badly readable one-liners, this can work:
", ".join(f"{brand} {model}" for brand, models in dict(cars).items() for model in models)
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)
The way you do this depends on exactly what the output should look like if the input data are slightly different to that shown in the question.
The data look like this:
data = [['Audi', ['A4']], ['Skoda', ['Superb']]]
This is interesting because there’s no apparent reason for the model designation to be in a list. The implication is that the data might look something like this:
data = [['Audi', ['A4', 'Series II']], ['Skoda', ['Superb']]]
So let’s allow for that construct…
def process(_list):
return ','.join(' '.join([make] + model) for make, model in _list)
print(process(data))
Output:
Audi A4 Series II, Skoda Superb