0

I have two python lists A and B of equal length each containing only boolean values. Is it possible to get a third list C where C[i] = A[i] and B[i] for 0 <= i < len(A) without using loop?

I tried following

C = A and B

but probably it gives the list B

I also tried

C = A or B

which gives first list

I know it can easily be done using for loop in single line like C = [x and y for x, y in zip(A, B)].

5
  • If you don't want loops, you may want to have a look at numpy. Commented Aug 15, 2016 at 13:27
  • 1
    @ayhan Which also uses loops, just hides them in C code.. Commented Aug 15, 2016 at 13:27
  • @ShamshadAlam You will have to have a loop, either an implicit or an explicit one. Why does it matter? Commented Aug 15, 2016 at 13:29
  • Yes, but it is numpy that uses those loops, not the OP. Commented Aug 15, 2016 at 13:29
  • @DeepSpace It doesn't matter to me. I just wanted to check whether it is possible without loop or not. Commented Aug 15, 2016 at 13:36

4 Answers 4

2

You can do it without an explicit loop by using map, which performs the loop internally, at C speed. Of course, the actual and operation is still happening at Python speed, so I don't think it'll save much time (compared to doing essentially the same thing with Numpy, which can not only do the looping at C speed, it can do the and operation at C speed too. Of course, there's also the overhead of converting between native Python lists & Numpy arrays).

Demo:

from operator import and_ a = [0, 1, 0, 1] b = [0, 0, 1, 1] c = map(and_, a, b) print c 

output

[0, 0, 0, 1] 

Note that the and_ function performs a bitwise and operation, but that should be ok since you're operating on boolean values.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd recommend you use numpy to use these kind of predicates over arrays. Now, I don't think you can avoid loops to achieve what you want, but... if you don't consider mapping or enumerating as a form of looping, you could do something like this (C1):

A = [True, True, True, True] B = [False, False, True, True] C = [x and y for x, y in zip(A, B)] C1 = map(lambda (i,x): B[i] and x, enumerate(A)) C2 = [B[i] and x for i,x in enumerate(A)] print C==C1==C2 

Comments

0

Simple answer: you can't.

Except in the trivial way, which is by calling a function that does this for you, using a loop. If you want this kind of nice syntax you can use libraries as suggested: map, numpy, etc. Or you can write your own function.

If what you are looking for is syntactic convenience, Python does not allow overloading operators for built-in types such as list.

Oh, and you can use recursion, if that's "not a loop" for you.

Comments

0

Is it possible to get a third list C where C[i] = A[i] and B[i] for 0 <= i < len(A) without using loop?

Kind of:

class AndList(list): def __init__(self, A, B): self.A = A self.B = B def __getitem__(self, index): return self.A[index] and self.B[index] A = [False, False, True, True] B = [False, True, False, True] C = AndList(A, B) print isinstance(C, list) and all(C[i] == (A[i] and B[i]) for i in range(len(A))) 

Prints True.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.