python - What's the fastest way to loop through a list and create a single string? -
for example:
list = [{"title_url": "joe_white", "id": 1, "title": "joe white"}, {"title_url": "peter_black", "id": 2, "title": "peter black"}]
how can efficiently loop through create:
joe white, peter black <a href="/u/joe_white">joe white</a>,<a href="/u/peter_black">peter black</a>
thank you.
the first pretty simple:
', '.join(item['title'] item in list)
the second requires more complicated, same:
','.join('<a href="/u/%(title_url)s">%(title)s</a>' % item item in list)
both use generator expressions, similar list comprehensions without need list creation
Comments
Post a Comment