- Related Questions & Answers
- Python program using the map function to find a row with the maximum number of 1's
- Python program using map function to find row with maximum number of 1's
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Find the largest rectangle of 1’s with swapping of columns allowed in Python
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Maximum length of segments of 0’s and 1’s in C++
- 1’s and 2’s complement of a Binary Number?
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Program to find number of bit 1 in the given number in Python
- Program to find the nth row of Pascal's Triangle in Python
- Find the maximum number of composite summands of a number in Python
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we are going to write a program which finds a row with a maximum number of 1's from a matrix using the **map **function.

Let's say we have the following matrix.

matrix = [ [0, 0, 1], [1, 1, 1], [1, 1, 0] ]

We can write a program in different ways. But, using **map **function, we will follow the below procedure.

- Initialise the matrix.
- Find the number of 1's in every row using
**map**function. Store them in a list. - Print the max from the list.

## initializing the matrix matrix = [ [0, 0, 1], [1, 1, 1], [1, 1, 0] ] ## function to find number of 1's in a row def number_of_ones(row): count = 0 for i in row: if i is 1: count += 1 return count ## finding the number of 1's in every row ## map returns an object which we converted into a list ones_count = list(map(number_of_ones, matrix)) ## printing the index of max number from the list print(ones_count.index(max(ones_count)))

If you run the above program, you will get the following result.

1

If you have any doubts regarding the program, please do mention them in the comment section.

Advertisements