w3resource logo


Python Exercises

Python Data Type: List - Exercises, Practice, Solution

Secondary Nav

Python List [71 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python program to sum all the items in a list. Go to the editor
Click me to see the sample solution

2. Write a Python program to multiplies all the items in a list. Go to the editor
Click me to see the sample solution

3. Write a Python program to get the largest number from a list. Go to the editor
Click me to see the sample solution

4. Write a Python program to get the smallest number from a list. Go to the editor
Click me to see the sample 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. Go to the editor
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
Click me to see the sample solution

6. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples. Go to the editor
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
Click me to see the sample solution

7. Write a Python program to remove duplicates from a list. Go to the editor
Click me to see the sample solution

8. Write a Python program to check a list is empty or not. Go to the editor
Click me to see the sample solution

9. Write a Python program to clone or copy a list. Go to the editor
Click me to see the sample solution

10. Write a Python program to find the list of words that are longer than n from a given list of words. Go to the editor
Click me to see the sample solution

11. Write a Python function that takes two lists and returns True if they have at least one common member. Go to the editor
Click me to see the sample solution

12. Write a Python program to print a specified list after removing the 0th, 2nd, 4th and 5th elements. Go to the editor
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']
Click me to see the sample solution

13. Write a Python program to generate a 3*4*6 3D array whose each element is *. Go to the editor
Click me to see the sample solution

14. Write a Python program to print the numbers of a specified list after removing even numbers from it. Go to the editor
Click me to see the sample solution

15. Write a Python program to shuffle and print a specified list. Go to the editor
Click me to see the sample solution

16. 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 (both included). Go to the editor
Click me to see the sample solution

17. Write a Python program to generate and print a list except for the first 5 elements, where the values are square of numbers between 1 and 30 (both included). Go to the editor
Click me to see the sample solution

18. Write a Python program to generate all permutations of a list in Python. Go to the editor
Click me to see the sample solution

19. Write a Python program to get the difference between the two lists. Go to the editor
Click me to see the sample solution

20. Write a Python program access the index of a list. Go to the editor
Click me to see the sample solution

21. Write a Python program to convert a list of characters into a string. Go to the editor
Click me to see the sample solution

22. Write a Python program to find the index of an item in a specified list. Go to the editor
Click me to see the sample solution

23. Write a Python program to flatten a shallow list. Go to the editor
Click me to see the sample solution

24. Write a Python program to append a list to the second list. Go to the editor
Click me to see the sample solution

25. Write a Python program to select an item randomly from a list. Go to the editor
Click me to see the sample solution

26. Write a python program to check whether two lists are circularly identical. Go to the editor
Click me to see the sample solution

27. Write a Python program to find the second smallest number in a list. Go to the editor
Click me to see the sample solution

28. Write a Python program to find the second largest number in a list. Go to the editor
Click me to see the sample solution

29. Write a Python program to get unique values from a list. Go to the editor
Click me to see the sample solution

30. Write a Python program to get the frequency of the elements in a list. Go to the editor
Click me to see the sample solution

31. Write a Python program to count the number of elements in a list within a specified range. Go to the editor
Click me to see the sample solution

32. Write a Python program to check whether a list contains a sublist. Go to the editor
Click me to see the sample solution

33. Write a Python program to generate all sublists of a list. Go to the editor
Click me to see the sample solution

34. Write a Python program using Sieve of Eratosthenes method for computing primes upto a specified number. Go to the editor
Note: In mathematics, the sieve of Eratosthenes, (Ancient Greek: κόσκινον Ἐρατοσθένους, kóskinon Eratosthénous) one of a number of prime number sieves, is a simple, ancient algorithm for finding all prime numbers up to any given limit.
Click me to see the sample solution

35. Write a Python program to create a list by concatenating a given list which range goes from 1 to n. Go to the editor
Sample list : ['p', 'q']
n =5
Sample Output : ['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4', 'p5', 'q5']
Click me to see the sample solution

36. Write a Python program to get variable unique identification number or string. Go to the editor
Click me to see the sample solution

37. Write a Python program to find common items from two lists. Go to the editor
Click me to see the sample solution

38. Write a Python program to change the position of every n-th value with the (n+1)th in a list. Go to the editor
Sample list: [0,1,2,3,4,5]
Expected Output: [1, 0, 3, 2, 5, 4]
Click me to see the sample solution

39. Write a Python program to convert a list of multiple integers into a single integer. Go to the editor
Sample list: [11, 33, 50]
Expected Output: 113350
Click me to see the sample solution

40. Write a Python program to split a list based on first character of word. Go to the editor
Click me to see the sample solution

41. Write a Python program to create multiple lists. Go to the editor
Click me to see the sample solution

42. Write a Python program to find missing and additional values in two lists. Go to the editor
Sample data : Missing values in second list: b,a,c
Additional values in second list: g,h
Click me to see the sample solution

43. Write a Python program to split a list into different variables. Go to the editor
Click me to see the sample solution

44. Write a Python program to generate groups of five consecutive numbers in a list. Go to the editor
Click me to see the sample solution

45. Write a Python program to convert a pair of values into a sorted unique array. Go to the editor
Click me to see the sample solution

46. Write a Python program to select the odd items of a list. Go to the editor
Click me to see the sample solution

47. Write a Python program to insert an element before each element of a list. Go to the editor
Click me to see the sample solution

48. Write a Python program to print a nested lists (each list on a new line) using the print() function. Go to the editor
Click me to see the sample solution

49. Write a Python program to convert list to list of dictionaries. Go to the editor
Sample lists: ["Black", "Red", "Maroon", "Yellow"], ["#000000", "#FF0000", "#800000", "#FFFF00"]
Expected Output: [{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_code': '#FF0000'}, {'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name': 'Yellow', 'color_code': '#FFFF00'}]
Click me to see the sample solution

50. Write a Python program to sort a list of nested dictionaries. Go to the editor
Click me to see the sample solution

51. Write a Python program to split a list every Nth element. Go to the editor
Sample list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
Expected Output: [['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']]
Click me to see the sample solution

52. Write a Python program to compute the similarity between two lists. Go to the editor
Sample data: ["red", "orange", "green", "blue", "white"], ["black", "yellow", "green", "blue"]
Expected Output:
Color1-Color2: ['white', 'orange', 'red']
Color2-Color1: ['black', 'yellow']
Click me to see the sample solution

53. Write a Python program to create a list with infinite elements. Go to the editor
Click me to see the sample solution

54. Write a Python program to concatenate elements of a list. Go to the editor
Click me to see the sample solution

55. Write a Python program to remove key values pairs from a list of dictionaries. Go to the editor
Click me to see the sample solution

56. Write a Python program to convert a string to a list. Go to the editor
Click me to see the sample solution

57. Write a Python program to check if all items of a list is equal to a given string. Go to the editor
Click me to see the sample solution

58. Write a Python program to replace the last element in a list with another list. Go to the editor
Sample data : [1, 3, 5, 7, 9, 10], [2, 4, 6, 8]
Expected Output: [1, 3, 5, 7, 9, 2, 4, 6, 8]
Click me to see the sample solution

59. Write a Python program to check if the n-th element exists in a given list. Go to the editor
Click me to see the sample solution

60. Write a Python program to find a tuple, the smallest second index value from a list of tuples. Go to the editor
Click me to see the sample solution

61. Write a Python program to create a list of empty dictionaries. Go to the editor
Click me to see the sample solution

62. Write a Python program to print a list of space-separated elements. Go to the editor
Click me to see the sample solution

63. Write a Python program to insert a given string at the beginning of all items in a list. Go to the editor
Sample list : [1,2,3,4], string : emp
Expected output : ['emp1', 'emp2', 'emp3', 'emp4']
Click me to see the sample solution

64. Write a Python program to iterate over two lists simultaneously. Go to the editor
Click me to see the sample solution

65. Write a Python program to access dictionary keys element by index. Go to the editor
Click me to see the sample solution

66. Write a Python program to find the list in a list of lists whose sum of elements is the highest. Go to the editor
Sample lists: [1,2,3], [4,5,6], [10,11,12], [7,8,9]
Expected Output: [10, 11, 12]
Click me to see the sample solution

67. Write a Python program to find all the values in a list are greater than a specified number. Go to the editor
Click me to see the sample solution

68. Write a Python program to extend a list without append. Go to the editor
Sample data: [10, 20, 30]
[40, 50, 60]
Expected output : [40, 50, 60, 10, 20, 30]
Click me to see the sample solution

69. Write a Python program to remove duplicates from a list of lists. Go to the editor
Sample list : [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]]
New List : [[10, 20], [30, 56, 25], [33], [40]]
Click me to see the sample solution

70. Write a Python program to get the depth of a dictionary. Go to the editor
Click me to see the sample solution

71. Write a Python program to check if all dictionaries in a list are empty or not. Go to the editor
Sample list : [{},{},{}]
Return value : True
Sample list : [{1,2},{},{}]
Return value : False
Click me to see the sample solution

Solution





More to Come !



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program