attrs = [o.attr for o in objs] was the right code for making a list like the one you describe. Don't try to subclass list for this. Is there something you did not like about that snippet?
loops - Extract list of attributes from list of objects in python - Stack Overflow
python - Find object in list that has attribute equal to some value (that meets any condition) - Stack Overflow
python - How do i access and object attribute inside a list of objects given certain index? - Stack Overflow
Searching a list of objects in Python - Stack Overflow
Hey, I often need a way of find an object in a list of objects in python. What is the best way to do that with a one liner ;)
eg. a list of invoice objects.. now I need to find and return an invoice with invoice.invoice_number = 1234
attrs = [o.attr for o in objs] was the right code for making a list like the one you describe. Don't try to subclass list for this. Is there something you did not like about that snippet?
You can also write:
attr=(o.attr for o in objsm)
This way you get a generator that conserves memory. For more benefits look at Generator Expressions.
next((x for x in test_list if x.value == value), None)
This gets the first item from the list that matches the condition, and returns None if no item matches. It's my preferred single-expression form.
However,
for x in test_list:
if x.value == value:
print("i found it!")
break
The naive loop-break version, is perfectly Pythonic -- it's concise, clear, and efficient. To make it match the behavior of the one-liner:
for x in test_list:
if x.value == value:
print("i found it!")
break
else:
x = None
This will assign None to x if you don't break out of the loop.
A simple example: We have the following array
li = [{"id":1,"name":"ronaldo"},{"id":2,"name":"messi"}]
Now, we want to find the object in the array that has id equal to 1
- Use method
nextwith list comprehension
next(x for x in li if x["id"] == 1 )
- Use list comprehension and return first item
[x for x in li if x["id"] == 1 ][0]
- Custom Function
def find(arr , id):
for x in arr:
if x["id"] == id:
return x
find(li , 1)
Output all the above methods is {'id': 1, 'name': 'ronaldo'}
In Python, to access object attribute the syntax is
<object instance>.<attribute_name>
In your case, [index].
As Paul said, use dir(<object_instance>[index]) to get all attribute, methods associated object instance.
You can also use for loop to iterate over
for <obj> in <object_list>:
<obj>.<attribute_name>
Hope it helps
You can access the dict (a.k.a Hashtable) of all attributes of an object with:
ListObj[i].__dict__
Or you can only get the names of these attributes as a list with either
ListObj[i].__dict__.keys()
and
dir(ListObj[i])
You can get a list of all matching elements with a list comprehension:
[x for x in myList if x.n == 30] # list of all elements with .n==30
If you simply want to determine if the list contains any element that matches and do it (relatively) efficiently, you can do
def contains(list, filter):
for x in list:
if filter(x):
return True
return False
if contains(myList, lambda x: x.n == 3) # True if any element has .n==3
# do stuff
Simple, Elegant, and Powerful:
A generator expression in conjuction with a builtinโฆ (python 2.5+)
any(x for x in mylist if x.n == 10)
Uses the Python any() builtin, which is defined as follows:
any(iterable)
->Return True if any element of the iterable is true. Equivalent to:
def any(iterable):
for element in iterable:
if element:
return True
return False