Python : List - Exercises and Solution


1. Write a Python program to sum all the items

Sample Output

[1,7,-10,34,2,-8]

Sum all the items = 26

View Solution

2. Write a Python program to multiply all the items in a list

Sample Output

[3,4,5,4,7]

Multiply all the items in a list : 1680

View Solution

3. Write a Python program to get the largest number from a list

Sample Output

[1,7,10,34,2,8]

Largest Number : 34

View Solution

4. Write a Python program to get the smallest number from a list

Sample Output

[51,7,10,34,2,8]

Smallest Number : 2

View Solution

5. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings

Sample Output

['abc', 'xyz', 'aba', '1221']

First and Last Character are same : 2

View Solution

6. Write a Python program to remove duplicates from a list

Sample Output

[1,2,3,7,2,1,5,6,4,8,5,4]

{1,2,3,4,5,6,7,8}

View Solution

7. Write a Python program to check a list is empty or not

Sample Output

[34,45,6,5,4,56,7]

List is Not Empty

View Solution

8. Write a Python program to clone or copy a list

Sample Output

[10, 22, 44, 23, 4]

Clone or Copy a List : [10, 22, 44, 23, 4]

View Solution

9. Write a Python program to find the list of words that are longer than n from a given list of words

Sample Output

Find the List of Words that are Longer than n from a given List of Words

Given value of n = 4

['Words', 'Longer', 'given', 'Words']

View Solution

10. Write a Program that get two lists as input and check if they have at least one common member

Sample Output

[1,2,3,4,5]

[5,6,7,8,9]

Lists have at least one common member

View Solution

11. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements. (enumerate)

Sample Output

["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]

['Dog', 'Elephant', 'Fox', 'Ponda']

View Solution

12. Write a Python program to print the numbers of a specified list after removing even numbers from it

Sample Output

[7,32,81,20,25,14,23,27]

[7, 81, 25, 23, 27]

View Solution

13. Write a Python program to shuffle and print a specified list (shuffle)

Sample Output

["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]

['Fox', 'Cat', 'Tiger', 'Lion', 'Dog', 'Ponda', 'Elephant']

View Solution

14. Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30

Sample Output

First 5 elements : [1, 4, 9, 16, 25]

Last 5 elements : [625, 676, 729, 784, 841]

View Solution

15. Write a Python program to generate all permutations of a list in Python. (itertools)

Sample Output

[1,2,3]

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

View Solution

16. Write a Python program to convert a list of characters into a string

Sample Output

['T','u','t','o','r',' ','J','o','e','s']

Tutor Joes

View Solution

17. Write a Python program to find the index of an item in a specified list

Sample Output

[20, 70, 30, 90, 10, 30, 90, 10, 80]

Item to find the index of 30

Index Number of Item = 2

View Solution

18. Write a Python program to flatten a shallow list

Sample Output

[[20,30,70],[30,90,10], [30,20], [70,90,10,80]]

[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]

View Solution

19. Write a Python program to add a list to the second list

Sample Output

[10, 20, 30, 40]

["Cat", "Dog", "Lion", "Ponda"]

[10, 20, 30, 40, 'Cat', 'Dog', 'Lion', 'Ponda']

View Solution

20. Write a Python program to select an item randomly from a list Using random.choice()

Sample Output

["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]

Item randomly from a list : Fox

View Solution

21. Write a python program to check whether two lists are circularly identical

Sample Output

[8, 8, 12, 12, 8]

[8, 8, 8, 12, 12]

[1, 8, 8, 12, 12]

Compare List1 and List2 : True

Compare List1 and List3 : False

View Solution

22. Write a Python program to find the second smallest number in a list

Sample Output

[2,4,56,78,4,34,5,8,9]

Second Smallest Number : 4

View Solution

23. Write a Python program to find the second largest number in a list

Sample Output

[82,4,56,78,4,34,5,100,9]

Second Largest Number : 82

View Solution

24. Write a Python program to get unique values from a list

Sample Output

[82, 4, 10, 56, 78, 4, 34, 5, 10, 9]

[34, 4, 5, 9, 10, 78, 82, 56]

View Solution

25. Write a Python program to get the frequency of the elements in a list.

Sample Output

[10, 30, 50, 10, 20, 60, 20, 60, 40, 40, 50, 50, 30]

Counter({50: 3, 10: 2, 30: 2, 20: 2, 60: 2, 40: 2})

View Solution

26. Create a list by concatenating a given list which range goes from 1 to n

Sample Output

['T', 'J']

N = 10

['T1', 'J1', 'T2', 'J2', 'T3', 'J3', 'T4', 'J4', 'T5', 'J5', 'T6', 'J6', 'T7', 'J7', 'T8', 'J8', 'T9', 'J9', 'T10', 'J10']

View Solution

27. Write a Python program to get variable unique identification number or string

Sample Output

x = 30

s = "Tutor Joes"

Unique Identification Number : 7005f980

Unique Identification String : c24bb0

View Solution

28. Write a Python program to find common items from two lists

Sample Output

[23,45,67,78,89,34]

[34,89,55,56,39,67]

Common items from two lists : {89, 34, 67}

View Solution

29. Write a Python program to split a list based on first character of word

Sample Output

["cat", "dog", "cow", "tiger", "lion", "Fox", "Shark", "Snake", "turtle", "mouse", "monkey", "bear"]

F
     Fox
S
     Shark
     Snake
b
     bear
c
     cat
     cow
d
     dog
l
     lion
m
     monkey
     mouse
t
     tiger
     turtle

View Solution

30. Write a Python program to select the odd number of a list

Sample Output

[1,2,4,3,6,7,5,8,9,7,8,9,10]

[1, 3, 7, 5, 9, 7, 9]

View Solution

31. Write a Python Program to count unique values inside a list

Sample Output

[10, 20, 30, 50, 80, 70, 70, 80, 10]

No of Unique Items in List : 6

View Solution

32. Write a Python Program to List product excluding duplicates

Sample Output

[2, 1, 2, 4, 6, 4, 3, 2, 1]

Duplication removal list product : 144

View Solution

33. Write a Python Program to Extract elements with Frequency greater than K

Sample Output

[4, 6, 4, 3, 3, 4, 3, 7, 8, 8]

View Solution

34. Write a Python Program to Test if List contains elements in Range

Sample Output

[4, 5, 6, 7, 3, 9]

Does list contain all elements in range : True

View Solution

35. Write a Python program to check if the list contains three consecutive common numbers in Python

Sample Output

[18, 18, 18, 6, 3, 4, 9, 9, 9]

Three Consecutive common numbers = 18, 9

View Solution

36. Write a Python program to find the Strongest Neighbour

Sample Output

[10,20,30,20,30,400]

20 30 30 30 400

View Solution

37. Write a Python Program to print all Possible Combinations from the three Digits

Sample Output

[1, 2, 3]

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

View Solution

38. Write a Python program to find all the Combinations in the list with the given condition

Sample Output

['Tutor Joes', ['Software', 'Computer'], ['Solution', 'Education']]

[ ['Tutor Joes', 'Software', 'Solution'], ['Tutor Joes', 'Computer', 'Education'] ]

View Solution

39. Write a Python program to get all unique combinations of two Lists

Sample Output

['A','B','C']

[1,2,3]

[ [('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)] ]

View Solution

40. Write a Python program to remove all the occurrences of an element from a list

Sample Output

[1, 3, 4, 6, 5, 1]

[3, 4, 6, 5]

View Solution

41. Write a Python Program to Remove Consecutive K element records

Sample Output

[ ('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'), ('C', 'C', 'G', 'F') ]

[ ('A', 'B', 'C', 'D'), ('H', 'D', 'B', 'C') ]

View Solution

42. Write a Python Program to Replace index elements with elements in Other List

Sample Output

['Tutor Joes', 'Computer', 'Education']

[2, 1, 0, 1, 0, 2, 2, 0, 1, 0, 1, 2]

['Education', 'Computer', 'Tutor Joes', 'Computer', 'Tutor Joes', 'Education', 'Education', 'Tutor Joes', 'Computer', 'Tutor Joes', 'Computer', 'Education']

View Solution

43. Write a Python Program to Retain records with N occurrences of K

Sample Output

[ (4, 5, 6, 5, 4), (4, 5, 3), (5, 5, 2), (3, 4, 9) ]

K = 5

N = 2

[ (4, 5, 6, 5, 4), (5, 5, 2) ]

View Solution

44. Write a Python Program to Swap elements in String list

Sample Output

['Tutor', 'joes', 'Computer', 'Education']

['Tutor', "Joe's", 'Software', 'Solutions']

View Solution

45. Write a Python program to reverse All Strings in String List

Sample Output

Original list = ['Tutor', 'joes', 'Computer', 'Education']

Reversed list = ['rotuT', 'seoj', 'retupmoC', 'noitacudE']

Reversed list = ['Education', 'Computer', 'joes', 'Tutor']

View Solution

46. Write a Python program to find the character position of Kth word from a list of strings

Sample Output

['Tutor', 'joes', 'Computer', 'Education']

K = 20

Index of character at Kth position word : 3

View Solution

47. Write a Python Program to Prefix frequency in string List

Sample Output

['TjC', 'TjCpp', 'TjPython', 'Java']

Prefix = 'Tj'

Strings count with matching frequency : 3

View Solution

48. Write a Python Program to Split Strings on Prefix Occurrence

Sample Output

['TjC', 'TjCpp', 'TjPython', 'Java', 'tj']

Prefix = 'Tj'

[ ['TjC'], ['TjCpp'], ['TjPython', 'Java', 'tj'] ]

View Solution

49. Write a Python program to Replace all Characters of a List Except the given character

Sample Output

['P', 'Y', 'T', 'H', 'O', 'N']

['@', '@', 'T', '@', '@', '@']

View Solution

50. Write a Python Program to Add Space between Potential Words

Sample Output

['TutorJoes', 'ComputerEducations']

[' Tutor Joes', ' Computer Educations']

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial