What is the difference between len() and count() in python?
list.count()
counts how many times the given value appears. You created a list of 5 elements that are all the same, so of course x_list.count()
finds that element 5 times in a list of length 5.
You could have tried the same test with a list with a mix of values:
>>> sample = [2, 10, 1, 1, 5, 2]
>>> len(sample)
6
>>> sample.count(1)
2
The sample
list contains 6 elements, but the value 1
appears only twice.