Python – Count elements in list [duplicate] – Dev

The best answers to the question “Python – Count elements in list [duplicate]” in the category Dev.

QUESTION:

I am trying to find a simple way of getting a count of the number of elements in a list:

MyList = ["a", "b", "c"]

I want to know there are 3 elements in this list.

ANSWER:

just do len(MyList)

This also works for strings, tuples, dict objects.

ANSWER:

len()

>>> someList=[]
>>> print len(someList)
0

ANSWER:

len() 

it will count the element in the list, tuple and string and dictionary,
eg.

>>> mylist = [1,2,3] #list
>>> len(mylist)
3
>>> word = 'hello' # string 
>>> len(word)
5
>>> vals = {'a':1,'b':2} #dictionary
>>> len(vals)
2
>>> tup = (4,5,6) # tuple 
>>> len(tup)
3

To learn Python you can use byte of python , it is best ebook for python beginners.

ANSWER:

len(myList) should do it.

len works with all the collections, and strings too.