Use len(), e.g.
>>> l = [1,2,3,4,5,6,7]
>>> print len(l)
7
Given the basic nature of your question, I recommend that you read the Python Tutorial.
Answer from mhawke on Stack OverflowI don't quite understand how this len() function works when it's applied to something such as this: len({1,2,3,4,5} & {2,5,7}) = 2. And when I change it somethign such as len({1,2,3,4,6} & {2,5,7}) =3
Use len(), e.g.
>>> l = [1,2,3,4,5,6,7]
>>> print len(l)
7
Given the basic nature of your question, I recommend that you read the Python Tutorial.
Use the len function :
len([1,2,3,4,5,6,7])
If you want to learn more about the len function : http://www.tutorialspoint.com/python/list_len.htm
You don't and do not need to.
Python lists grow and shrink dynamically as needed to fit their contents. Sets are implemented as a hash table, and like Python dictionaries grow and shrink dynamically as needed to fit their contents.
Perhaps you were looking for collections.deque (which takes a maxlen parameter) or something using a heapq (using heapq.heappushpop() when you have reached the maximum) instead?
Once you have your list, lst, you can
if len(lst)>10:
lst = lst[:10]
If size more than 10 elements, you truncate to first ten elements.