# How Do I convert an int array of 0s and 1s into Boolean in numpy?

**URL:** <https://forum.freecodecamp.org/t/how-do-i-convert-an-int-array-of-0s-and-1s-into-boolean-in-numpy/460941>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [May 19, 2021, 12:08pm UTC](https://forum.freecodecamp.org/t/how-do-i-convert-an-int-array-of-0s-and-1s-into-boolean-in-numpy/460941 "2021-05-19T12:08:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geek-Killer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/geek-killer/32/63972_2.png) [@Geek-Killer](https://forum.freecodecamp.org/u/Geek-Killer)\
**Post date:** [May 19, 2021, 12:08pm UTC](https://forum.freecodecamp.org/t/how-do-i-convert-an-int-array-of-0s-and-1s-into-boolean-in-numpy/460941/1 "2021-05-19T12:08:48Z")

</div>

let’s say I have this:

a = np.array([0,0,1,0,0],[1,0,0,1,0], [1,0,1,0,0])

I would like to convert a into boolean, how do I do this?

a.astype(bool) dosn’t work since it is a multidimensional array and it needs to be done without a for loop

---

<div class="post-metadata">

**Author:** ![AndreasGuldborg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andreasguldborg/32/205683_2.png) [@AndreasGuldborg](https://forum.freecodecamp.org/u/AndreasGuldborg)\
**Post date:** [May 19, 2021, 12:13pm UTC](https://forum.freecodecamp.org/t/how-do-i-convert-an-int-array-of-0s-and-1s-into-boolean-in-numpy/460941/2 "2021-05-19T12:13:35Z")

</div>

Hey!  
I am not the very best at using maps, but I think that is what you might be seeking here:

> **[Python map() function - GeeksforGeeks](https://www.geeksforgeeks.org/python-map-function/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

I think it only works on lists that are always the same dimensions (in other words, I’m not sure if you can make a general mapping function for arrays that are sometimes one-dimensional, other times four-dimensional).

I hope it makes sense and helps you 🙂

---

<div class="post-metadata">

**Author:** ![Jagaya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jagaya/32/202277_2.png) [@Jagaya](https://forum.freecodecamp.org/u/Jagaya)\
**Post date:** [May 19, 2021, 1:06pm UTC](https://forum.freecodecamp.org/t/how-do-i-convert-an-int-array-of-0s-and-1s-into-boolean-in-numpy/460941/3 "2021-05-19T13:06:43Z")

</div>

```auto
a = np.array([[0,0,1,0,0],[1,0,0,1,0], [1,0,1,0,0]])
a.astype(np.bool)
# output:
array([[False, False, True, False, False],
       [True, False, False, True, False],
       [True, False, True, False, False]])

```

Looks to me as though it worked perfectly.  
It’s a wild guess, but I guess because np.array only has one dedicated data-type for all it’s entrys - you don’t need to loop.

Like, Python arrays have a datatype for every single entry. That’s why you can mix everything in them.  
np.arrays in part are super efficient, because they have basic values in each cell and only one-datatype entry that is applied to read out the values. So in return, changing that type via `.astype()` will automatically apply to all entries.  
But again - that’s just my guess.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [November 18, 2021, 1:07am UTC](https://forum.freecodecamp.org/t/how-do-i-convert-an-int-array-of-0s-and-1s-into-boolean-in-numpy/460941/4 "2021-11-18T01:07:01Z")

</div>

Closed on November 18, 2021, 1:07am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
