1

I have the following list in Python:

my_list = ["yes", "yes", "no", "yes", "no", "yes"]

I want to return a list of logical values, being True in case the corresponding list element in my_list is equal to "yes" and False in case the corresponding list element in my_list is equal to "no".

Desired output:

my_list_logical = [True, True, False, True, False, True]

I'm usually programming in R, and in R this could be done easily with the following code:

my_list == "yes"

I found a Python solution in this thread. However, the solution of this thread is very complicated, and I cannot believe that this is so much more complicated in Python than in R.

Is there a simple way on how to return a list of logicals based on my input vector?

Wimpel
  • 26,031
  • 1
  • 20
  • 37
Joachim Schork
  • 2,025
  • 3
  • 25
  • 48

5 Answers5

4

You can use list comprehension:

my_list = ["yes", "yes", "no", "yes", "no", "yes"]

logical = [item=='yes' for item in my_list]

OUTPUT:

[True, True, False, True, False, True]

Or, you can even use map:

logical = list(map(lambda x: x=='yes', my_list))
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
3

Probably the best answer is to use the package numpy. Numpy arrays act similarly to R vectors.

import numpy as np
my_list = np.array(["yes", "yes", "no", "yes", "no", "yes"])
my_list == "yes"
array([ True,  True, False,  True, False,  True])
David Kaftan
  • 1,974
  • 1
  • 12
  • 18
  • It feels rather over-engineered to pull in numpy just to run a list comprehension/equivalent of `map`. – Kemp Jun 01 '21 at 14:04
  • Well, if you want behavior similar to R, thats what you have to do. If you are coming from R you are probably working in data so I would imagine you already have numpy installed. – David Kaftan Jun 01 '21 at 14:07
  • The question was specifically about a regular Python list and they didn't ask about a way to make it identical to R, just a simple way to do it. A list comprehension is simple and provides the result they asked for. I'm not saying the answer should be removed or anything, but it's probably not going to get much interest. – Kemp Jun 01 '21 at 14:10
  • Fair enough, I think using numpy is simpler, particularly if you come from R. – David Kaftan Jun 01 '21 at 14:12
2

You could use map() function with syntax map(<function>, <iterable>). However you will have to code it into a list() to yield the results, for example:

my_list = ["yes", "yes", "no", "yes", "no", "yes"]
my_list_logical = list(map(lambda x: x == "yes", my_list))
print(my_list_logical)

Output

[True, True, False, True, False, True]
blackraven
  • 5,284
  • 7
  • 19
  • 45
1

You may very much use a list comprehension:

my_list = ["yes", "yes", "no", "yes", "no", "yes"]
result = [item == "yes" for item in my_list]

Which yields

[True, True, False, True, False, True]
Jan
  • 42,290
  • 8
  • 54
  • 79
1

Yes and this is the most logical way I can think of

my_list = ["yes", "yes", "no", "yes", "no", "yes"]
my_list_logical = []
for i in my_list:
   if i == “yes”:
      my_list_logical.append(True)
   else:
      my_list_logical.append(False)
Chis
  • 9
  • 1
  • 4