Eric Crowley Eric Crowley - 1 year ago 63
Python Question

Generate String From List

I want to take the values from the following list and insert them into a string. This is the format of the list:

[{u'level': {u'status': u'Automatic', u'lastReportedAt': u'2016-05-09T06:37:26Z', u'lowPercent': 10, u'highPercent': 10}, u'color': u'Black', u'type': u'Economy', u'carId': u'319142c0-c2dc-4498-8cb0-0cc9babdee56', u'engineNumber': u'24B6035', u'rrfType': u'Cab'}

The output string should be something like this:

"Your car is an Automatic, Black, Economy".

The "Automatic", "Black", and "Economy" values are all values from the list. This will be running in Python 2.7.

Answer Source
for car in carList:
    print("Your car is a {}, {}, {}.").format(car['level']['status'], car['color'], car['type'])

Edit to address the comment

t = ""
for car in carList:
    t += "Your car is a {}, {}, {}. ".format(car['level']['status'], car['color'], car['type'])