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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -