Python 2d array boolean reduction

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












I've got a 2D array comprised of boolean values (True,False). I'd like to consolidate the array to a 1D based on a logical function of the contents.



e.g.
Input:



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


Output (logical AND):



[False,
False,
True]


How would this be done without a loop ?










share|improve this question



























    up vote
    7
    down vote

    favorite
    1












    I've got a 2D array comprised of boolean values (True,False). I'd like to consolidate the array to a 1D based on a logical function of the contents.



    e.g.
    Input:



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


    Output (logical AND):



    [False,
    False,
    True]


    How would this be done without a loop ?










    share|improve this question

























      up vote
      7
      down vote

      favorite
      1









      up vote
      7
      down vote

      favorite
      1






      1





      I've got a 2D array comprised of boolean values (True,False). I'd like to consolidate the array to a 1D based on a logical function of the contents.



      e.g.
      Input:



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


      Output (logical AND):



      [False,
      False,
      True]


      How would this be done without a loop ?










      share|improve this question















      I've got a 2D array comprised of boolean values (True,False). I'd like to consolidate the array to a 1D based on a logical function of the contents.



      e.g.
      Input:



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


      Output (logical AND):



      [False,
      False,
      True]


      How would this be done without a loop ?







      python arrays reduction






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 58 mins ago









      Willem Van Onsem

      131k16125210




      131k16125210










      asked 1 hour ago









      DnRng

      735




      735






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          4
          down vote













          You can do this without NumPy too. Here is one solution using list comprehension. Explanation: It will loop over sub-lists and even if one of the items in each sub-list is False, it outputs False else True.



          inp = [[True, True, False],[False, False, False],[True, True, True]]
          out = [False if False in i else True for i in inp]
          print (out)

          # [False, False, True]


          Alternative (less verbose) as suggested by Jean below:



          out = [False not in i for i in inp]





          share|improve this answer






















          • out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
            – Jean-François Fabre
            59 mins ago










          • @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
            – Bazingaa
            58 mins ago











          • This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
            – timgeb
            44 mins ago










          • Well the OP said it’s only True or False so my sokution addressed that
            – Bazingaa
            12 mins ago

















          up vote
          4
          down vote













          You can use Python's built-in all method with a list-comprehension:



          [all(x) for x in my_list]


          If that's still too loopy for you, combine it with map:



          map(all, my_list)


          Note that map doesn't return a list in Python 3. If you want a list as your result, you can call list(map(all, my_list)) instead.






          share|improve this answer


















          • 1




            brilliant use of all
            – Jean-François Fabre
            57 mins ago










          • @Jean-FrançoisFabre Thank you very much
            – Woody1193
            56 mins ago










          • careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
            – Jean-François Fabre
            55 mins ago










          • @Jean-FrançoisFabre True, I'll make a note of it
            – Woody1193
            54 mins ago

















          up vote
          3
          down vote













          I'm assuming you want to apply logical ANDs to the rows. You can apply numpy.all.



          >>> import numpy as np
          >>> a = np.array([[True, True, False], [False, False, False], [True, True, True]])
          >>> a
          array([[ True, True, False],
          [False, False, False],
          [ True, True, True]])
          >>>
          >>> np.all(a, axis=1)
          array([False, False, True])


          For a solution without numpy, you can use operator.and_ and functools.reduce.



          >>> from operator import and_
          >>> from functools import reduce
          >>>
          >>> lst = [[True, True, False], [False, False, False], [True, True, True]]
          >>> [reduce(and_, sub) for sub in lst]
          [False, False, True]


          edit: actually, reduce is a bit redundant in this particular case.



          >>> [all(sub) for sub in lst]
          [False, False, True]


          does the job just as well.






          share|improve this answer





























            up vote
            2
            down vote













            You can do this with numpy with the numpy.all function:



            >>> import numpy as np
            >>> arr = np.array([[True, True, False],
            ... [False, False, False],
            ... [True, True, True]]
            ... )
            >>> np.all(arr, axis=1)
            array([False, False, True])


            Here thus the i-th element is True if all elements of the i-th row are True, and False otherwise. Note that the list should be rectangular (all sublists should contain the same number of booleans).



            In "pure" Python, you can use the all function as well, like:



            >>> data = [[True, True, False], [False, False, False], [True, True, True]]
            >>> list(map(all, data))
            [False, False, True]


            This approach will work as well if the "matrix" is not rectangular. Note that for an empty sublist, this will return True, since all elements in an empty sublist are True.






            share|improve this answer



























              up vote
              2
              down vote













              You can also do this with map and reduce:



              from functools import reduce

              l = [[True, True, False],
              [False, False, False],
              [True, True, True]]

              final = list(map(lambda x: reduce(lambda a, b: a and b, x), l))
              print(final)
              # [False, False, True]


              The benefit here is that you can change the reduce function to something else (say, an OR or something more adventurous).






              share|improve this answer






















                Your Answer





                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52823751%2fpython-2d-array-boolean-reduction%23new-answer', 'question_page');

                );

                Post as a guest






























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                4
                down vote













                You can do this without NumPy too. Here is one solution using list comprehension. Explanation: It will loop over sub-lists and even if one of the items in each sub-list is False, it outputs False else True.



                inp = [[True, True, False],[False, False, False],[True, True, True]]
                out = [False if False in i else True for i in inp]
                print (out)

                # [False, False, True]


                Alternative (less verbose) as suggested by Jean below:



                out = [False not in i for i in inp]





                share|improve this answer






















                • out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
                  – Jean-François Fabre
                  59 mins ago










                • @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
                  – Bazingaa
                  58 mins ago











                • This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
                  – timgeb
                  44 mins ago










                • Well the OP said it’s only True or False so my sokution addressed that
                  – Bazingaa
                  12 mins ago














                up vote
                4
                down vote













                You can do this without NumPy too. Here is one solution using list comprehension. Explanation: It will loop over sub-lists and even if one of the items in each sub-list is False, it outputs False else True.



                inp = [[True, True, False],[False, False, False],[True, True, True]]
                out = [False if False in i else True for i in inp]
                print (out)

                # [False, False, True]


                Alternative (less verbose) as suggested by Jean below:



                out = [False not in i for i in inp]





                share|improve this answer






















                • out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
                  – Jean-François Fabre
                  59 mins ago










                • @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
                  – Bazingaa
                  58 mins ago











                • This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
                  – timgeb
                  44 mins ago










                • Well the OP said it’s only True or False so my sokution addressed that
                  – Bazingaa
                  12 mins ago












                up vote
                4
                down vote










                up vote
                4
                down vote









                You can do this without NumPy too. Here is one solution using list comprehension. Explanation: It will loop over sub-lists and even if one of the items in each sub-list is False, it outputs False else True.



                inp = [[True, True, False],[False, False, False],[True, True, True]]
                out = [False if False in i else True for i in inp]
                print (out)

                # [False, False, True]


                Alternative (less verbose) as suggested by Jean below:



                out = [False not in i for i in inp]





                share|improve this answer














                You can do this without NumPy too. Here is one solution using list comprehension. Explanation: It will loop over sub-lists and even if one of the items in each sub-list is False, it outputs False else True.



                inp = [[True, True, False],[False, False, False],[True, True, True]]
                out = [False if False in i else True for i in inp]
                print (out)

                # [False, False, True]


                Alternative (less verbose) as suggested by Jean below:



                out = [False not in i for i in inp]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 58 mins ago

























                answered 1 hour ago









                Bazingaa

                6,9951822




                6,9951822











                • out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
                  – Jean-François Fabre
                  59 mins ago










                • @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
                  – Bazingaa
                  58 mins ago











                • This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
                  – timgeb
                  44 mins ago










                • Well the OP said it’s only True or False so my sokution addressed that
                  – Bazingaa
                  12 mins ago
















                • out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
                  – Jean-François Fabre
                  59 mins ago










                • @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
                  – Bazingaa
                  58 mins ago











                • This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
                  – timgeb
                  44 mins ago










                • Well the OP said it’s only True or False so my sokution addressed that
                  – Bazingaa
                  12 mins ago















                out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
                – Jean-François Fabre
                59 mins ago




                out = [False if False in i else True for i in inp] is a cumbersome way to write out = [False not in i for i in inp]
                – Jean-François Fabre
                59 mins ago












                @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
                – Bazingaa
                58 mins ago





                @Jean-FrançoisFabre: I added your suggestion too. Thanks :)
                – Bazingaa
                58 mins ago













                This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
                – timgeb
                44 mins ago




                This is clever but not exactly equivalent to x1 and x2 and ... xn for each sublist when you can have other falsy values in the sublists (such as 0). Of course, OP explicitly said boolean values, so this is just a sidenote.
                – timgeb
                44 mins ago












                Well the OP said it’s only True or False so my sokution addressed that
                – Bazingaa
                12 mins ago




                Well the OP said it’s only True or False so my sokution addressed that
                – Bazingaa
                12 mins ago












                up vote
                4
                down vote













                You can use Python's built-in all method with a list-comprehension:



                [all(x) for x in my_list]


                If that's still too loopy for you, combine it with map:



                map(all, my_list)


                Note that map doesn't return a list in Python 3. If you want a list as your result, you can call list(map(all, my_list)) instead.






                share|improve this answer


















                • 1




                  brilliant use of all
                  – Jean-François Fabre
                  57 mins ago










                • @Jean-FrançoisFabre Thank you very much
                  – Woody1193
                  56 mins ago










                • careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
                  – Jean-François Fabre
                  55 mins ago










                • @Jean-FrançoisFabre True, I'll make a note of it
                  – Woody1193
                  54 mins ago














                up vote
                4
                down vote













                You can use Python's built-in all method with a list-comprehension:



                [all(x) for x in my_list]


                If that's still too loopy for you, combine it with map:



                map(all, my_list)


                Note that map doesn't return a list in Python 3. If you want a list as your result, you can call list(map(all, my_list)) instead.






                share|improve this answer


















                • 1




                  brilliant use of all
                  – Jean-François Fabre
                  57 mins ago










                • @Jean-FrançoisFabre Thank you very much
                  – Woody1193
                  56 mins ago










                • careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
                  – Jean-François Fabre
                  55 mins ago










                • @Jean-FrançoisFabre True, I'll make a note of it
                  – Woody1193
                  54 mins ago












                up vote
                4
                down vote










                up vote
                4
                down vote









                You can use Python's built-in all method with a list-comprehension:



                [all(x) for x in my_list]


                If that's still too loopy for you, combine it with map:



                map(all, my_list)


                Note that map doesn't return a list in Python 3. If you want a list as your result, you can call list(map(all, my_list)) instead.






                share|improve this answer














                You can use Python's built-in all method with a list-comprehension:



                [all(x) for x in my_list]


                If that's still too loopy for you, combine it with map:



                map(all, my_list)


                Note that map doesn't return a list in Python 3. If you want a list as your result, you can call list(map(all, my_list)) instead.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 54 mins ago

























                answered 58 mins ago









                Woody1193

                1,295624




                1,295624







                • 1




                  brilliant use of all
                  – Jean-François Fabre
                  57 mins ago










                • @Jean-FrançoisFabre Thank you very much
                  – Woody1193
                  56 mins ago










                • careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
                  – Jean-François Fabre
                  55 mins ago










                • @Jean-FrançoisFabre True, I'll make a note of it
                  – Woody1193
                  54 mins ago












                • 1




                  brilliant use of all
                  – Jean-François Fabre
                  57 mins ago










                • @Jean-FrançoisFabre Thank you very much
                  – Woody1193
                  56 mins ago










                • careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
                  – Jean-François Fabre
                  55 mins ago










                • @Jean-FrançoisFabre True, I'll make a note of it
                  – Woody1193
                  54 mins ago







                1




                1




                brilliant use of all
                – Jean-François Fabre
                57 mins ago




                brilliant use of all
                – Jean-François Fabre
                57 mins ago












                @Jean-FrançoisFabre Thank you very much
                – Woody1193
                56 mins ago




                @Jean-FrançoisFabre Thank you very much
                – Woody1193
                56 mins ago












                careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
                – Jean-François Fabre
                55 mins ago




                careful with map in python 3 though, as it doesn't create a list (but is even better when used within a loop)
                – Jean-François Fabre
                55 mins ago












                @Jean-FrançoisFabre True, I'll make a note of it
                – Woody1193
                54 mins ago




                @Jean-FrançoisFabre True, I'll make a note of it
                – Woody1193
                54 mins ago










                up vote
                3
                down vote













                I'm assuming you want to apply logical ANDs to the rows. You can apply numpy.all.



                >>> import numpy as np
                >>> a = np.array([[True, True, False], [False, False, False], [True, True, True]])
                >>> a
                array([[ True, True, False],
                [False, False, False],
                [ True, True, True]])
                >>>
                >>> np.all(a, axis=1)
                array([False, False, True])


                For a solution without numpy, you can use operator.and_ and functools.reduce.



                >>> from operator import and_
                >>> from functools import reduce
                >>>
                >>> lst = [[True, True, False], [False, False, False], [True, True, True]]
                >>> [reduce(and_, sub) for sub in lst]
                [False, False, True]


                edit: actually, reduce is a bit redundant in this particular case.



                >>> [all(sub) for sub in lst]
                [False, False, True]


                does the job just as well.






                share|improve this answer


























                  up vote
                  3
                  down vote













                  I'm assuming you want to apply logical ANDs to the rows. You can apply numpy.all.



                  >>> import numpy as np
                  >>> a = np.array([[True, True, False], [False, False, False], [True, True, True]])
                  >>> a
                  array([[ True, True, False],
                  [False, False, False],
                  [ True, True, True]])
                  >>>
                  >>> np.all(a, axis=1)
                  array([False, False, True])


                  For a solution without numpy, you can use operator.and_ and functools.reduce.



                  >>> from operator import and_
                  >>> from functools import reduce
                  >>>
                  >>> lst = [[True, True, False], [False, False, False], [True, True, True]]
                  >>> [reduce(and_, sub) for sub in lst]
                  [False, False, True]


                  edit: actually, reduce is a bit redundant in this particular case.



                  >>> [all(sub) for sub in lst]
                  [False, False, True]


                  does the job just as well.






                  share|improve this answer
























                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    I'm assuming you want to apply logical ANDs to the rows. You can apply numpy.all.



                    >>> import numpy as np
                    >>> a = np.array([[True, True, False], [False, False, False], [True, True, True]])
                    >>> a
                    array([[ True, True, False],
                    [False, False, False],
                    [ True, True, True]])
                    >>>
                    >>> np.all(a, axis=1)
                    array([False, False, True])


                    For a solution without numpy, you can use operator.and_ and functools.reduce.



                    >>> from operator import and_
                    >>> from functools import reduce
                    >>>
                    >>> lst = [[True, True, False], [False, False, False], [True, True, True]]
                    >>> [reduce(and_, sub) for sub in lst]
                    [False, False, True]


                    edit: actually, reduce is a bit redundant in this particular case.



                    >>> [all(sub) for sub in lst]
                    [False, False, True]


                    does the job just as well.






                    share|improve this answer














                    I'm assuming you want to apply logical ANDs to the rows. You can apply numpy.all.



                    >>> import numpy as np
                    >>> a = np.array([[True, True, False], [False, False, False], [True, True, True]])
                    >>> a
                    array([[ True, True, False],
                    [False, False, False],
                    [ True, True, True]])
                    >>>
                    >>> np.all(a, axis=1)
                    array([False, False, True])


                    For a solution without numpy, you can use operator.and_ and functools.reduce.



                    >>> from operator import and_
                    >>> from functools import reduce
                    >>>
                    >>> lst = [[True, True, False], [False, False, False], [True, True, True]]
                    >>> [reduce(and_, sub) for sub in lst]
                    [False, False, True]


                    edit: actually, reduce is a bit redundant in this particular case.



                    >>> [all(sub) for sub in lst]
                    [False, False, True]


                    does the job just as well.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 53 mins ago

























                    answered 1 hour ago









                    timgeb

                    38.8k105177




                    38.8k105177




















                        up vote
                        2
                        down vote













                        You can do this with numpy with the numpy.all function:



                        >>> import numpy as np
                        >>> arr = np.array([[True, True, False],
                        ... [False, False, False],
                        ... [True, True, True]]
                        ... )
                        >>> np.all(arr, axis=1)
                        array([False, False, True])


                        Here thus the i-th element is True if all elements of the i-th row are True, and False otherwise. Note that the list should be rectangular (all sublists should contain the same number of booleans).



                        In "pure" Python, you can use the all function as well, like:



                        >>> data = [[True, True, False], [False, False, False], [True, True, True]]
                        >>> list(map(all, data))
                        [False, False, True]


                        This approach will work as well if the "matrix" is not rectangular. Note that for an empty sublist, this will return True, since all elements in an empty sublist are True.






                        share|improve this answer
























                          up vote
                          2
                          down vote













                          You can do this with numpy with the numpy.all function:



                          >>> import numpy as np
                          >>> arr = np.array([[True, True, False],
                          ... [False, False, False],
                          ... [True, True, True]]
                          ... )
                          >>> np.all(arr, axis=1)
                          array([False, False, True])


                          Here thus the i-th element is True if all elements of the i-th row are True, and False otherwise. Note that the list should be rectangular (all sublists should contain the same number of booleans).



                          In "pure" Python, you can use the all function as well, like:



                          >>> data = [[True, True, False], [False, False, False], [True, True, True]]
                          >>> list(map(all, data))
                          [False, False, True]


                          This approach will work as well if the "matrix" is not rectangular. Note that for an empty sublist, this will return True, since all elements in an empty sublist are True.






                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            You can do this with numpy with the numpy.all function:



                            >>> import numpy as np
                            >>> arr = np.array([[True, True, False],
                            ... [False, False, False],
                            ... [True, True, True]]
                            ... )
                            >>> np.all(arr, axis=1)
                            array([False, False, True])


                            Here thus the i-th element is True if all elements of the i-th row are True, and False otherwise. Note that the list should be rectangular (all sublists should contain the same number of booleans).



                            In "pure" Python, you can use the all function as well, like:



                            >>> data = [[True, True, False], [False, False, False], [True, True, True]]
                            >>> list(map(all, data))
                            [False, False, True]


                            This approach will work as well if the "matrix" is not rectangular. Note that for an empty sublist, this will return True, since all elements in an empty sublist are True.






                            share|improve this answer












                            You can do this with numpy with the numpy.all function:



                            >>> import numpy as np
                            >>> arr = np.array([[True, True, False],
                            ... [False, False, False],
                            ... [True, True, True]]
                            ... )
                            >>> np.all(arr, axis=1)
                            array([False, False, True])


                            Here thus the i-th element is True if all elements of the i-th row are True, and False otherwise. Note that the list should be rectangular (all sublists should contain the same number of booleans).



                            In "pure" Python, you can use the all function as well, like:



                            >>> data = [[True, True, False], [False, False, False], [True, True, True]]
                            >>> list(map(all, data))
                            [False, False, True]


                            This approach will work as well if the "matrix" is not rectangular. Note that for an empty sublist, this will return True, since all elements in an empty sublist are True.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            Willem Van Onsem

                            131k16125210




                            131k16125210




















                                up vote
                                2
                                down vote













                                You can also do this with map and reduce:



                                from functools import reduce

                                l = [[True, True, False],
                                [False, False, False],
                                [True, True, True]]

                                final = list(map(lambda x: reduce(lambda a, b: a and b, x), l))
                                print(final)
                                # [False, False, True]


                                The benefit here is that you can change the reduce function to something else (say, an OR or something more adventurous).






                                share|improve this answer


























                                  up vote
                                  2
                                  down vote













                                  You can also do this with map and reduce:



                                  from functools import reduce

                                  l = [[True, True, False],
                                  [False, False, False],
                                  [True, True, True]]

                                  final = list(map(lambda x: reduce(lambda a, b: a and b, x), l))
                                  print(final)
                                  # [False, False, True]


                                  The benefit here is that you can change the reduce function to something else (say, an OR or something more adventurous).






                                  share|improve this answer
























                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    You can also do this with map and reduce:



                                    from functools import reduce

                                    l = [[True, True, False],
                                    [False, False, False],
                                    [True, True, True]]

                                    final = list(map(lambda x: reduce(lambda a, b: a and b, x), l))
                                    print(final)
                                    # [False, False, True]


                                    The benefit here is that you can change the reduce function to something else (say, an OR or something more adventurous).






                                    share|improve this answer














                                    You can also do this with map and reduce:



                                    from functools import reduce

                                    l = [[True, True, False],
                                    [False, False, False],
                                    [True, True, True]]

                                    final = list(map(lambda x: reduce(lambda a, b: a and b, x), l))
                                    print(final)
                                    # [False, False, True]


                                    The benefit here is that you can change the reduce function to something else (say, an OR or something more adventurous).







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 51 mins ago

























                                    answered 58 mins ago









                                    slider

                                    2,900926




                                    2,900926



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52823751%2fpython-2d-array-boolean-reduction%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Displaying single band from multi-band raster using QGIS

                                        How many registers does an x86_64 CPU actually have?