HassanK
HassanK

Reputation: 80

Perform any operation on two consecutive values of the list

I have a list

truthList = [True,False,False,False,True,False,True,True]

I want to "and" the two subsequent indexes of this list.

output[0]=truthList[0] and truthList[1]
output[1]=truthList[2] and truthList[3]
....

"Anding" the above list will give the following output

output = [False,False,False,True]

There are two cases

1 List has only even number of indexes

2 List can have even or odd number of indexes

Can someone please suggest an easy shortcut for both of these cases(preferably through list comprehension or maps)

Upvotes: 1

Views: 97

Answers (3)

dobhareach
dobhareach

Reputation: 344

One brute force way to approach this would be to just append the last item back onto the end of the list if we have an odd number of elements. That way we get l[n - 1] and l[n - 1] for the last element in the output list:

l = [True, False, False, False, True, False, True, True]
if len(l) % 2 == 1:
    l.append(l[-1])
l_sort = [ l[i] and l[i + 1] for i in range(0, len(l), 2)] 
print(l_sort)

Upvotes: 2

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

For both even and odd length list, you may use itertools.izip_longest() with list comprehesnion expression as:

>>> from itertools import izip_longest
>>> truthList = [True,False,False,False,True,False,True,True, False]

#                           jumping the list by 2  v                    
>>> [i and j for i, j in izip_longest(truthList[::2], truthList[1::2], fillvalue=False)]
#     Does `and` with `False` as pair up for last entry in case of `odd` list ^
[False, False, False, True, False]

Here, you need to set the value of fillvalue parameter as True/False based on the behavior you need. As the document say:

If the iterables are of uneven length, missing values are filled-in with fillvalue.

For odd length list, if you want to keep the last value of list as it is, then you have to set the fillvalue as truthList[-1].


For just even list, zip will suffice the requirement as:

>>> truthList = [True,False,False,False,True,False,True,True]

>>> [i and j for i, j in zip(truthList[::2], truthList[1::2])]
[False, False, False, True]

Upvotes: 2

ettanany
ettanany

Reputation: 19806

You can use map() with operator.and_() like below:

import operator

map(operator.and_, truthList[::2], truthList[1::2])

If your list length is an odd number, you can do:

result = map(operator.and_, truthList[:-1:2], truthList[1:-1:2])

Then, if you want to add False for the last value:

result.append(False)

The complete code would be:

def my_and(my_list):
    if len(my_list) % 2:
        result = map(operator.and_, my_list[:-1:2], my_list[1:-1:2])
        result.append(False)
        return result
    return map(operator.and_, my_list[::2], my_list[1::2])

Output:

>>> truth_list1 = [True,False,False,False,True,False,True,True]
>>> my_and(truth_list1)
[False, False, False, True]
>>> 
>>> truth_list2 = [True,False,False,False,True,False,True,True,True]
>>> my_and(truth_list2)
[False, False, False, True, False]

Upvotes: 1

Related Questions