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>>> source_list = [('1','a'),('2','b'),('3','c'),('4','d')]
2>>> list1, list2 = zip(*source_list)
3>>> list1
4('1', '2', '3', '4')
5>>> list2
6('a', 'b', 'c', 'd')
7
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