1#List of Tuples
2list_tuples = [('Nagendra',18),('Nitesh',28),('Sathya',29)]
3
4#To print the list of tuples using for loop you can print by unpacking them
5for name,age in list_tuples:
6 print(name,age)
7
8#To print with enumerate--->enumerate is nothing but gives the index of the array.
9for index,(name,age) in list_tuples:
10 #print using fstring
11 print(f'My name is {name} and age is {age} and index is {index}')
12 #print using .format
13 print('My name is {n} and age is {a} and index is {i}'.format(n=name,a=age,i=index))
14
1#A tuple is essentailly a list with limited uses. They are popular when making variables
2#or containers that you don't want changed, or when making temporary variables.
3#A tuple is defined with parentheses.
1import string
2fhand = open('romeo-full.txt')
3counts = dict()
4for line in fhand:
5 line = line.translate(None, string.punctuation)
6 line = line.lower()
7 words = line.split()
8 for word in words:
9 if word not in counts:
10 counts[word] = 1
11 else:
12 counts[word] += 1
13
14# Sort the dictionary by value
15lst = list()
16for key, val in counts.items():
17 lst.append( (val, key) )
18
19lst.sort(reverse=True)
20
21for key, val in lst[:10] :
22 print key, val
23