Creating a generator expression from a list in python -


What's the best way to do the following in Python:

  for items [x X.attr for some_list x]: do_something_with (item)  

This may be a short question, but the understanding of the list is not needed to create a new list, for which Not required and take the memory? Would not it be better if we could understand an Iterator-like list?

Yes (both of your questions)

By using brackets instead of brackets, you can say "generator expression" for that sequence, which is proposed by you. It allows you to retain the sequence without assigning a list to keep all the elements together.

  in item (x for x for some in x): do_something_with (item)  

The details of the generator expression are documented inside.


Comments

Popular posts from this blog

java - Is there an object like a "Set" that can contain only unique string values, but also contain a count on the number of occurrences of the string value? -

MVP, design question -