How to access attributes of classes in list without a for loop? I've been creating lists and looping through them is inefficient so am trying to avoid if so for clean code.
kill_the_loop.py
# Define our arbitrary class
class IntRelationships(object):
def __init__(self, a, b):
self.a = a
self.b = b
self.diff = abs(b-a)
def add(self):
return self.a + self.b
# create a class list
relationship_list = [ IntRelationships(1,2), IntRelationships(2,4), IntRelationships(4, 8)]
# How I currently access attributes in list
large_diff_list = []
for relationship in relationship_list:
if relationship.diff > 1:
large_diff_list.append(relationship)
# How imagine one could do it without a loop and array initialisation
large_diff_list2 = relationship_list[ relationship_list[:].diff > 1 ]