I am quite new to Python (have a C++ background). I have a function in my Python code that returns an array of some objects (on which I don't have any control) like below:
def _get_object_array():
object_array[]
# Magically get array contents from somewhere
# The objects have some fields like id, name etc.
return object_array
Then in another function I call _get_object_array() to retrieve only the id's of the objects returned.
What I have is following:
id_array = []
id_array = [x.id for x in _get_object_array()] #<--
But I don't want the ids of all the objects in the returned array. Instead I only want to store the ids for array elements which meet some criteria (e.g. id is an even number?)
Is there a way I can do that in the same line marked with <-- like below (which btw is just a pseudo code)?
id_array = [if x.id % 2 == 0: x.id for x in _get_object_array()]
Thanks in advance.
/R