Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

How to count a number of even integers in a nested list

aishwarya chandupatla

What is a nested list?

A nested list is a list whose elements are lists:

Example

Let l be the list and l1, l2, and l3 be the sub lists:

l1 = [1,2,3] 
l2 = [4,5,6] 
l3 = [7,8,9]

l = [[1,2,3],[4,5,6],[7,8,9]]

Code

l = [[1,2,8], [3,5,5], [4,6,6]]

count=0

for i in l:
    for j in i:
        if(j%2==0):
            count=count+1
print(count)     

Explanation

  1. A nested list, l, is taken with sub-lists.

  2. Count variable is used to count the number of even numbers in the list and is initialized to zero.

  3. The nested for loop is used to access the elements in the list.

  4. Variable i is used to access sub-lists in the list, and variable j is used to access elements in sub-list i.

  5. Th even condition is checked with j and, if it is satisfied, the count value is incremented by one.

  6. Finally, the count valuerepresents the number of even numbers in a nested list is printed.

RELATED TAGS

python
community creator

CONTRIBUTOR

aishwarya chandupatla
RELATED COURSES

View all Courses

Keep Exploring