list and tuples code example
Example 1: python list of tuples
list_tuples = [('Nagendra',18),('Nitesh',28),('Sathya',29)]
for name,age in list_tuples:
print(name,age)
for index,(name,age) in list_tuples:
print(f'My name is {name} and age is {age} and index is {index}')
print('My name is {n} and age is {a} and index is {i}'.format(n=name,a=age,i=index))
Example 2: list vs tuple python
mylist : list = []
mytuple : tuple = ()
Example 3: 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