I'm new to Python and come from a Java background. I'd like to know the most Pythonic way of writing this code:
entry_list = []
for entry in feed.entry:
entry_list.append(entry.title.text)
Basically for each element in the feed, I'd like to append that element's title to a list.
I don't know if I should use a map() or lambda function or what...
Thanks
most pythonic code I can think of:
entry_list = [entry.title.text for entry in feed.entry]
This is a list comprehension which will construct a new list out of the elements in feed.entry.title.text.
To append you will need to do:
entry_list.extend([entry.title.text for entry in feed.entry])
As a side note, when doing extend operations, the normally fast generator expression is much slower than a list comprehension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With