It’s complicated. Selectattr allows us to select items based on an attributes value. In this case our object is a list of length 2 tuples. When pulling out information from a tuple, we need to use an index. 0, being the first item, 1 being the second and so on. So the selectattr is iterating the list and looking at the tuple value where index=1. Then selectattr only selects the object, if the value of index 1 equals True.
So a more clear example would be this:
{% set mylist = [
('a', False),
('b', True),
('c', False) ] %}
So if i only want to get a tuple that where the 2nd item is True, I would use the following selectattr
{{ mylist | selectattr('1','eq', True) | list }}
the output would be
[('b', True)]
If i only wanted a list of items that are false:
{{ mylist | selectattr('1','eq', False) | list }}
the output would be
[('a', False), ('c', False) ]
If i only wanted letters equal to ‘a’:
{{ mylist | selectattr('0','eq', 'a') | list }}
the output would be
[('a', False)]
Now you can get real fancy with select attr. What if i wanted ‘a’ and ‘b’, regardless of the True/False?
{{ mylist | selectattr('0','in', ['a','b']) | list }}
the output would be
[('a', False), ('b', True)]
Now you can expand upon this with all sorts of data types. I just chose a list of tuples because it’s the easiest to get a pair of information, like a day number and True/False flag.
This is my data structure
{% set days = [ (0, sun), (1, mon), (2, tue), (3, wed), (4, thur), (5, fri), (6, sat) ] %}
This filters days after today that are on.
{% set days_after = days | selectattr('1','eq',True) | selectattr('0','>', weekday) | list %}
This filters out days before today including today that are on
{% set days_before = days | selectattr('1','eq',True) | selectattr('0','<=', weekday) | list %}