List and tuple code example
Example 1: list of tuples python
import string
fhand = open('romeo-full.txt')
counts = dict()
for line in fhand:
line = line.translate(None, string.punctuation)
line = line.lower()
words = line.split()
for word in words:
if word not in counts:
counts[word] = 1
else:
counts[word] += 1
lst = list()
for key, val in counts.items():
lst.append( (val, key) )
lst.sort(reverse=True)
for key, val in lst[:10] :
print key, val
Example 2: python tuple and list time compare
import timeit
print(timeit.timeit('x=(1,2,3,4,5,6,7,8,9)', number=100000))
print(timeit.timeit('x=[1,2,3,4,5,6,7,8,9]', number=100000))