List Based Programs PDF
List Based Programs PDF
SET - 01
Output:
Sum all the items = 26
Output:
Multiply all the items in a list: 1680
Output:
Largest Number: 34
Output:
Smallest Number: 2
5. WAPP to count the number of strings where the string length is 2 or more
and the first and last character are same from a give list of strings
Input:
['abc','xyz','aba','1221']
Output:
Find and Last Character are same: 2
6. WAPP to remove duplicates from a list
Input:
[1,2,3,7,2,1,5,6,4,8,5,4]
Output:
[1,2,3,4,5,6,7,8]
Output:
List is Not Empty
Output:
Clone or Copy a List: [10,22,44,23,4]
9. WAPP to find the list of words that are longer than n from a given list of
words.
Input:
Find the List of Words that are Longer than n from a given List of Words Given value of n = 4
Output:
['Words', 'Longer', 'given', 'Words']
10. WAPP that get two lists as input and check if they have at least one
common member.
Input:
[1,2,3,4,5] [5,6,7,8,9]
Output:
Lists have atleast one common member
SOLUTION -- SET 01
In [90]: 1 # 01
2 item=[1,7,-10,34,2,-8]
3 sum = 0
4 for i in item:
5 sum += i
6 print(sum)
26
In [91]: 1 # 02
2 item=[3,4,5,4,7]
3 total = 1
4 for i in item:
5 total *= i
6 print(total)
1680
In [92]: 1 # 03
2 a=[1,7,10,34,2,8]
3 print("Largest Number :",max(a))
Largest Number : 34
Largest Number : 34
In [94]: 1 # 04
2 a=[51,7,10,34,2,8]
3 print("Smallest Number :",min(a))
Smallest Number : 2
Smallest Number : 2
In [96]: 1 # 05
2 """
3 Sample List: ['abc', 'xyz', 'aba', '1221']
4 Expected Result: 2
5 """
6
7 word=["madem","3643","apple","3756"]
8 ch = 0
9 for w in word:
10 if len(w) > 1 and w[0] == w[-1]:
11 ch += 1
12 print(ch)
In [97]: 1 # 06
2 a = [1,2,3,7,2,1,5,6,4,8,5,4]
3 b=set(a)
4 print(list(b))
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
In [99]: 1 # 07
2 a = []
3 if not a:
4 print("List is Empty...")
5 else:
6 print("List is Not Empty...")
List is Empty...
In [100]: 1 # 08
2 old_list = [10, 22, 44, 23, 4]
3 new_list = list(old_list)
4
5 print("Old List : ",old_list)
6 print("New List : ",new_list)
In [102]: 1 # 10
2 list1=[1,2,3,4,5]
3 list2=[5,6,7,8,9]
4
5 result = False
6 for x in list1:
7 for y in list2:
8 if x == y:
9 result = True
10 print(result)
11 if result:
12 print("Lists have at least one common member")
13 else:
14 print("Lists do not have any common member")
True
Lists have at least one common member
SET - 02
11. WAPP to print a specified list after removing the 0th, 4th and 5th elements.
(enumerate)
Input:
["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
Output:
['Dog', 'Elephant', 'Fox', 'Ponda']
12. WAPP to print the numbers of a specified list after removing even numbers
from it
Input:
[7,32,81,20,25,14,23,27]
Output:
[7, 81, 25, 23, 27]
13. WAPP to shuffle and print a specified list (shuffle)
Input:
["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
Output:
['Fox', 'Cat', 'Tiger', 'Lion', 'Dog', 'Ponda', 'Elephant']
14. WAPP to generate and print a list of first and last 5 elements where the
values are square of numbers between 1 and 30
Output:
First 5 elements : [1, 4, 9, 16, 25]
Last 5 elements : [625, 676, 729, 784, 841]`
Output:
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
Output:
DPS Ruby
Output:
Item to find the index of 30
Output:
[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]
19. WAPP to add a list to the second list
Input:
[10, 20, 30, 40]
Output:
[10, 20, 30, 40, 'Cat', 'Dog', 'Lion', 'Ponda']
Output:
Item randomly from a list : Fox
SOLUTION -- SET 02
In [103]: 1 # 11
2 animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
3 a = []
4 for i, x in enumerate(animal):
5 if i not in (0,4,5):
6 a.append(x)
7 print(a)
In [105]: 1 # 12
2 n = [7,32,81,20,25,14,23,27]
3 n1 = []
4 for x in n:
5 if x%2 != 0:
6 n1.append(x)
7 print(n1)
In [108]: 1 # 14
2 l = list()
3 for i in range(1,25):
4 l.append(i**2)
5 print(l[:5])
6 print(l[-5:])
In [109]: 1 # 15
2 import itertools
3 print(list(itertools.permutations([1,2,3])))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
In [110]: 1 # 16
2 s = ['D','P','S',' ', 'R','u','b','y']
3 s1 = ''.join(s)
4 print(s1)
DPS Ruby
In [111]: 1 # 17
2 num = [20, 70, 30, 90, 10, 30, 90, 10, 80]
3 print(num.index(30))
In [112]: 1 # 18
2 ori_list = [[20,30,70],[30,90,10], [30,20], [70,90,10,80]]
3 flatten = []
4 for i in ori_list:
5 for j in i:
6 flatten.append(j)
7 print(flatten)
[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]
[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]
In [114]: 1 # 19
2 Number = [10, 20, 30, 40]
3 animal = ["Cat", "Dog", "Lion", "Ponda"]
4 final_list = Number + animal
5 print(final_list)
In [115]: 1 # 20
2 import random
3 animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Panda"]
4 print(random.choice(animal))
Elephant
SET - 03
Output:
Compare List1 and List2 : True
Output:
Second Smallest Number : 4
Output:
Second Largest Number : 82
Output:
{50: 3, 10: 2, 30: 2, 20: 2, 60: 2, 40: 2}
26. Create a list by concatenating a given list which range goes from 1 to n
Input:
['T', 'J']
N = 10
Output:
['T1', 'J1', 'T2', 'J2', 'T3', 'J3', 'T4', 'J4', 'T5', 'J5', 'T6', 'J6', 'T7', 'J7', 'T8', 'J8', 'T9', 'J9', 'T10', 'J10']
s = "DPS Ruby"
Output:
Unique Identification Number : 7005f980
[34,89,55,56,39,67]
Output:
Common items from two lists : {89, 34, 67}
Output:
F
Fox
S
Shark
Snake
b
bear
c
cat
cow
d
dog
l
lion
m
monkey
mouse
t
tiger
turtle
Output:
[1, 3, 7, 5, 9, 7, 9]
SOLUTION -- SET 03
In [116]: 1 # 21
2 list1 = [8, 8, 12, 12, 8]
3 list2 = [8, 8, 8, 12, 12]
4 list3 = [1, 8, 8, 12, 12]
5
6 print("Compare List1 and List2 : ",' '.join(map(str, list2)) in ' '.join(map(str, li
7 print("Compare List1 and List3 : ",' '.join(map(str, list3)) in ' '.join(map(str, li
In [118]: 1 # 23
2 num = [ 82,4,56,78,4,34,5,100,9]
3
4 if (len(num)<2):
5 print(num)
6
7 if ((len(num)==2) and (num[0] == num[1]) ):
8 print(num)
9
10 dup_items = set()
11 uniq_items = []
12 for x in num:
13 if x not in dup_items:
14 uniq_items.append(x)
15 dup_items.add(x)
16 uniq_items.sort()
17 print(uniq_items[-2])
82
In [119]: 1 # 24
2 l = [ 82,4,10,56,78,4,34,5,10,9]
3
4 print("Original List : ",l)
5
6 s = set(l)
7 new_list = list(s)
8
9 print("Unique Numbers : ",new_list)
Original List : [10, 30, 50, 10, 20, 60, 20, 60, 40, 40, 50, 50, 30]
Frequency of the Elements: Counter({50: 3, 10: 2, 30: 2, 20: 2, 60: 2, 40: 2})
In [121]: 1 # 26
2 ch = ['T', 'J']
3 n = 10
4 new_list = ['{}{}'.format(a, b) for b in range(1, n+1) for a in ch]
5 print(new_list)
['T1', 'J1', 'T2', 'J2', 'T3', 'J3', 'T4', 'J4', 'T5', 'J5', 'T6', 'J6', 'T7', 'J7', 'T
8', 'J8', 'T9', 'J9', 'T10', 'J10']
In [122]: 1 # 27
2 x = 30
3 print(format(id(x), 'x'))
4 s = "DPS Ruby"
5 print(format(id(s), 'x'))
7ffcb4a0d6c8
2136278fdb0
In [123]: 1 # 28
2 num1 = [23,45,67,78,89,34]
3 num2 = [34,89,55,56,39,67]
4 print(set(num1) & set(num2))
F
Fox
S
Shark
Snake
b
bear
c
cat
cow
d
dog
l
lion
m
monkey
mouse
t
tiger
turtle
In [125]: 1 # 30
2 a=[1,2,4,3,6,7,5,8,9,7,8,9,10]
3 odd_num=[]
4
5 for i in a:
6 if(i%2==1):
7 #print(i)
8 odd_num.append(i)
9
10 print(odd_num)
[1, 3, 7, 5, 9, 7, 9]
SET - 04
31. WAPP to count unique values inside a list
Input:
[10, 20, 30, 50, 80, 70, 70, 80, 10]
Output:
No of Unique Items in List : 6
Output:
Duplication removal list product : 144
Output:
Output:
Does list contain all elements in range : True
35. WAPP to check if the list contains three consecutive common numbers in
Python
Input:
[18, 18, 18, 6, 3, 4, 9, 9, 9]
Output:
Three Consecutive common numbers = 18, 9
Output:
20 30 30 30 400
37. WAPP to print all Possible Combinations from the three Digits
Input:
[1, 2, 3]
Output:
123
132
213
231
312
321
38. WAPP to find all the Combinations in the list with the given condition
Input:
['DPS Ruby', ['Software', 'Computer'], ['Solution', 'Education']]
Output:
[ ['DPS Ruby', 'Software', 'Solution'], ['DPS Ruby', 'Computer', 'Education'] ]
[1,2,3]
Output:
[ [('A', 1), ('B', 2), ('C', 3)], [('A', 1), ('C', 2), ('B', 3)], [('B', 1), ('A', 2), ('C', 3)], [('B', 1), ('C', 2), ('A', 3)], [('C', 1),
('A', 2), ('B', 3)], [('C', 1), ('B', 2), ('A', 3)] ]
Output:
[3, 4, 6, 5]
SOLUTION -- SET 04
In [126]: 1 # 31
2 #First Method
3
4 a = [10, 20, 30, 50, 80, 70, 70, 80, 10]
5 s = set(a)
6 print("No of Unique Items in List :", len(s))
In [127]: 1 # 31
2 # Second Method
3 a = [10, 20, 30, 50, 80, 70, 70, 80, 10]
4 l = []
5 count = 0
6 for i in a:
7 if i not in l:
8 count += 1
9 l.append(i)
10
11 print("No of Unique Items in List :", count)
In [128]: 1 # 32
2 a = [2,1,2,4,6,4,3,2,1]
3 print ("Original list : " + str(a))
4
5 b=list(set(a))
6 p=1
7 for i in b:
8 p*=i
9
10 print("Duplication removal list product : " + str(p))
In [129]: 1 # 33
2 a = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
3 print("Original list : " + str(a))
4 K = 2
5
6 res = []
7 for i in a:
8 freq = a.count(i)
9 if freq > K and i not in res:
10 res.append(i)
11
12 print("The Required Elements : " + str(res))
In [131]: 1 # 35
2 # creating the aay
3 a = [18, 18, 18, 6, 3, 4, 9, 9, 9]
4 l = len(a)
5 for i in range(l - 2):
6 if a[i] == a[i + 1] and a[i + 1] == a[i + 2]:
7 print(a[i])
18
9
In [132]: 1 # 36
2 n = 6
3 a1 = [10,20,30,20,30,400]
4 a2 = []
5 for i in range(1, n):
6 r = max(a1[i], a1[i-1])
7 a2.append(r)
8 for i in a2 :
9 print(i,end=" ")
20 30 30 30 400
In [133]: 1 # 37
2 a = [1, 2, 3]
3 for i in range(3):
4 for j in range(3):
5 for k in range(3):
6 if (i!=j and j!=k and i!=k):
7 print(a[i], a[j], a[k])
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
In [134]: 1 # 38
2 val = ["DPS Ruby",["Software","Computer"], ["Solution", "Education"]]
3 print("Original List : " + str(val))
4 a = 2
5 l = []
6 c = 0
7
8 while c <= a - 1:
9 t = []
10 for i in val:
11 if not isinstance(i, list):
12 t.append(i)
13 else:
14 t.append(i[c])
15 c += 1
16 l.append(t)
17
18 print("\nIndex Combinations : " + str(l))
Index Combinations : [['DPS Ruby', 'Software', 'Solution'], ['DPS Ruby', 'Computer', 'E
ducation']]
In [135]: 1 # 39
2 import itertools
3 from itertools import permutations
4 l1 = ['A','B','C']
5 l2 = [1,2,3]
6 unique = []
7 permut = itertools.permutations(l1, len(l2))
8 for comb in permut:
9 zipped = zip(comb, l2)
10 unique.append(list(zipped))
11
12 print(unique)
[[('A', 1), ('B', 2), ('C', 3)], [('A', 1), ('C', 2), ('B', 3)], [('B', 1), ('A', 2),
('C', 3)], [('B', 1), ('C', 2), ('A', 3)], [('C', 1), ('A', 2), ('B', 3)], [('C', 1),
('B', 2), ('A', 3)]]
In [136]: 1 # 40
2 val = [1, 3, 4, 6, 5, 1]
3 a = 1
4 print ("Original list :" ,val)
5
6 c = val.count(a)
7 for i in range(c):
8 val.remove(a)
9
10 print ("Remove operation :" , val)
SET - 05
41. WAPP to Remove Consecutive K element records
Input:
[ ('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'), ('C', 'C', 'G', 'F') ]
Output:
[ ('A', 'B', 'C', 'D'), ('H', 'D', 'B', 'C') ]
[2, 1, 0, 1, 0, 2, 2, 0, 1, 0, 1, 2]
Output:
['Education', 'Computer', 'DPS Ruby', 'Computer', 'DPS Ruby', 'Education', 'Education', 'DPS Ruby',
'Computer', 'DPS Ruby', 'Computer', 'Education']
K=5
N=2
Output:
[ (4, 5, 6, 5, 4), (5, 5, 2) ]
Output:
['DPS', "Ruby", 'Software', 'Solutions']
Output:
Reversed list : ['SPD', 'ybur', 'retupmoC', 'noitacudE']
46. WAPP to find the character position of Kth word from a list of strings
Input:
['DPS', 'ruby', 'Computer', 'Education']
K = 20
Output:
Index of character at Kth position word : 5
Prefix = 'Tj'
Output:
Strings count with matching frequency : 3
Prefix = 'Tj'
Output:
[ ['TjC'], ['TjCpp'], ['TjPython', 'Java', 'tj'] ]
49. WAPP to Replace all Characters of a List Except the given character
Input:
['P', 'Y', 'T', 'H', 'O', 'N']
Output:
['@', '@', 'T', '@', '@', '@']
Output:
[' D P S Ruby', ' Computer Educations']
SOLUTION -- SET 05
In [137]: 1 # 41
2 val = [('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'), ('C', 'C',
3 print("Original List : " + str(val))
4 K = 'C'
5 res = [i for i in val if not any(i[j] == K and i[j + 1] == K for j in range(len(i) -
6
7 print("After Removal : " , res)
Original List : [('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'),
('C', 'C', 'G', 'F')]
After Removal : [('A', 'B', 'C', 'D'), ('H', 'D', 'B', 'C')]
In [138]: 1 # 42
2 a = ['DPS', 'Computer', 'Education']
3 b = [2 ,1 ,0 ,1 ,0 ,2 ,2 ,0 ,1 ,0 ,1 ,2]
4 print("List 1 : " , a)
5 print("List 2 : " , b)
6 res = [a[i] for i in b]
7
8 print ("After Index Elements Replacements is : " ,res)
In [139]: 1 # 43
2 val = [(4, 5, 6, 5, 4), (4, 5, 3), (5, 5, 2), (3, 4, 9)]
3 print(val)
4 K = 5
5 N = 2
6 res = [e for e in val if e.count(K) == N]
7
8 print(res)
In [140]: 1 # 44
2 s = ["DPS","ruby","Computer","Education"]
3
4 print("Before Swap :",s)
5
6 res = [sub.replace("ruby","Ruby").replace("Computer", "Software").replace("Education
7
8 print ("After Swap : ",res)
In [144]: 1 # 46
2 val = ["DPS","ruby","Computer","Education"]
3 print("The original list is : " ,val)
4 K = 20
5 res = [i[0] for sub in enumerate(val) for i in enumerate(sub[1])]
6 res = res[K]
7 print("Index of character at Kth position word : " + str(res))
In [145]: 1 # 47
2 val = ["TjC","TjCpp","TjPython","Java"]
3 print("Original List : ",val)
4 sub = 'Tj'
5 res = 0
6 for e in val:
7 if e.startswith(sub):
8 res = res + 1
9 print ("Strings count with matching frequency : ",res)
In [147]: 1 # 49
2 val = ['P', 'Y', 'T', 'H', 'O', 'N']
3 print("The original list : " + str(val))
4
5 res = [i if i == 'T' else '@' for i in val]
6 print("List after replacement : " + str(res))
In [148]: 1 # 50
2 val = ["DPSRuby", "ComputerEducations"]
3 print("Original list : " ,val)
4
5 res = []
6 for i in val:
7 t = [[]]
8 for ch in i:
9 if ch.isupper():
10 t.append([])
11 t[-1].append(ch)
12 res.append(' '.join(''.join(i) for i in t))
13
14 print("The space added list of strings : " , res)