How to count the number of elements in a list

0 votes
May 27, 2019 in Python by Wajiha
• 1,960 points

retagged May 28, 2019 by Kalgi 5,422 views

1 answer to this question.

0 votes

To count the number of elements of a string, the len() method can be used.

Example:

a = ["x", "y", "z"]
len(a)

Output:

3
answered May 27, 2019 by Nisa
• 1,090 points
0 votes

1) Count total number of numbers in the list  

2) Sum and Average of all the numbers in the list  

3) Count and sum of all the odd numbers in the list  

4) Count and sum of all the even numbers in the list  

5) Find the largest number in the list  

6) Find the smallest number in the list  

Display all the values with appropriate titles.  

listNo = [6,8,10,44,33,21,7,1,0,2]

c = 0

s = 0

avg = 0

sOdd = 0

sEven = 0

cOdd = 0

cEven = 0

for i in listNo :

    c += 1

    s = s+i

    avg = s/c

    if i % 2 == 0 :

        sEven = sEven + i

        cEven = cEven + 1

    else :

        sOdd = sOdd + i

        cOdd = cOdd + 1

print ("total number of numbers in the list  : ", c)

print("sum of all numbers : ",s)

print("average of all numbers : ",avg)

print("count odd numbers : ",cOdd)

print("sum of odd numbers : ",sOdd)

print("count even numbers : ",cEven)

print("sum of odd numbers : ",sEven)

print("largest number in the list : " ,max(listNo))

print("smallest number in the list  : ",min(listNo))

answered Apr 14, 2021 by VISHAKHA AGARWAL

edited Mar 5

Related Questions In Python

0 votes
1 answer

How do I get the number of elements in a list?

Hello, The len() function can be used with several different ...READ MORE

answered Nov 18, 2020 in Python by Niroj
• 82,840 points
729 views
0 votes
1 answer

How do I get the number of elements in a list

items = [] items.append("apple") items.append("orange") items.append("banana") len(items) #use the ...READ MORE

answered Feb 15, 2022 in Python by Dev
• 6,000 points
834 views
0 votes
1 answer

How do I get the number of elements in a list

The len() function can be used with several different ...READ MORE

answered Feb 15, 2022 in Python by CoolCoder
• 4,420 points
732 views
0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
1,679 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,641 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,414 views
0 votes
1 answer

How to check if a list is empty in python?

To check if a list is empty ...READ MORE

answered May 27, 2019 in Python by Nisa
• 1,090 points
1,242 views
0 votes
4 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP