Python - Count elements in list [duplicate]

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.


Solution 1:

len()

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

Solution 2:

just do len(MyList)

This also works for strings, tuples, dict objects.

Solution 3:

len(myList) should do it.

len works with all the collections, and strings too.

Solution 4:

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.