Python List Programs, Exercises

Python list programs help beginners to become experts and the list is one of the most important data structures in Python Programming. Python list can contain any type of data as a member that we can access via positive and negative indexing.

In this article, we have two different sections one for Python list programs for beginners and the second for Python list projects for practice.

python list programs

Python List Programs

1). Python program to calculate the square of each number from the given list.

2). Python program to combine two lists.

3). Python program to calculate the sum of all elements from a list.

4). Python program to find a product of all elements from a given list.

5). Python program to find the minimum and maximum elements from the list.

6). Python program to differentiate even and odd elements from the given list.

7). Python program to remove all duplicate elements from the list.

8). Python program to print a combination of 2 elements from the list whose sum is 10.

9). Python program to print squares of all even numbers in a list.

10). Python program to split the list into two-part, the left side all odd values and the right side all even values.
Input = [5, 7, 2, 8, 11, 12, 17, 19, 22]
Output = [5, 7, 11, 17, 19, 2, 8, 12, 22]

11).  Python program to get common elements from two lists.
Input =
list1 = [4, 5, 7, 9, 2, 1]
list2 = [2, 5, 8, 3, 4, 7]
Outputt : [4, 5, 7, 2]

12). Python program to reverse a list with for loop.

13). Python program to reverse a list with a while loop.

14). Python program to reverse a list using index slicing.

15). Python program to reverse a list with reversed and reverse methods.

16). Python program to copy or clone one list to another list.

17). Python program to return True if two lists have any common member.

18). Python program to print a specific list after removing the 1st, 3rd, and 6th elements from the list.

19). Python program to remove negative values from the list.

20). Python program to get a list of all elements which are divided by 3 and 7.

21). Python program to check whether the given list is palindrome or not. (should be equal from both sides).

22). Python Program to get a list of words which has vowels in the given string.
Input: “www Student ppp are qqqq learning Python vvv”
Output : [‘Student’, ‘are’, ‘learning’, ‘Python’]

23). Python program to add 2 lists with extend method.

24). Python program to sort list data, with the sort and sorted method.

25). Python program to remove data from the list from a specific index using the pop method.

26). Python program to get the max, min, and sum of the list using in-built functions.

27). Python program to check whether a list contains a sublist.

28). Python program to generate all sublists with 5 or more elements in it from the given list.

29). Python program to find the second largest number from the list.
 

30). Python program to find the second smallest number from the list.

31). Python program to merge all elements of the list in a single entity using a special character.
 

32). Python program to get the difference between two lists.

33). Python program to reverse each element of the list.
Input = [‘Sqa’, ‘Tools’, ‘Online’, ‘Learning’, ‘Platform’]
output = [‘aqS’, ‘slooT’, ‘enilno’, ‘gninraeL’, ‘mroftalP’]
34). Python program to combine two list elements as a sublist in a list.
list1 = [3, 5, 7, 8, 9]
list2 = [1, 4, 3, 6, 2]
Output = [[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
35). Python program to get keys and values from the list of dictionaries.
Input : [{‘a’:12}, {‘b’: 34}, {‘c’: 23}, {‘d’: 11}, {‘e’: 15}]
Output :  [‘a’, ‘b’, ‘c’, ‘d’, ‘c’]
                [12, 34, 23, 11, 15]

36). Python program to get all the unique numbers in the list.

37). Python program to convert a string into a list.

38). Python program to replace the last and the first number of the list with the word.
Input: [12, 32, 33, 5, 4, 7]
output : [‘SQA’, 32, 33, 5, 4, ‘Tools’]
 

39). Python program to check whether the given element is exist in the list or not.

40). Python program to remove all odd index elements.
Input: [12, 32, 33, 5, 4, 7, 33]
Output: [12,33,4,33]

41). Python program to take two lists and return true if then at least one common member.

42). Python program to convert multiple numbers from a list into a single number.
Input: [12, 45, 56]
Output:124556
43). Python program to convert words of a list into a single string.
Input: [‘Sqa’, ‘Tools’, ‘Best’, ‘Learning’, ‘Platform’]
Output: SqaToolsBestLearningPlatform
44). Python program to print elements of the list separately.
Input: [(‘Black’, ‘Yellow’, ‘Blue’), (50, 55, 60), (30.0, 50.5, 55.66)]
Output:
(‘Black’, ‘Yellow’, ‘Blue’)
(50, 55, 60)
(30.0, 50.5, 55.66)
45). Python program to create a sublist of numbers and their squares from 1 to 10.
Output : [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]
46). Python program to create a list of five consecutive numbers in the list.
Output : [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
47). Python program to insert a given string at the beginning of all items in a list.
Input: [1, 2, 3, 4, 5], Sqa
Output: [‘Sqa1’, ‘Sqa2’, ‘Sqa3’, ‘Sqa4’, ‘Sqa5’]
48). Python program to iterate over two lists simultaneously and create a list of sublists.
list1 = [1, 3, 5, 7, 9]
list2 = [8, 6, 4, 2, 10]
output = [[1, 8], [3, 6], [5, 4], [7, 2], [9, 10]]
49). Python program to move all positive numbers on the left side and negative numbers on the right side.
Input: [2, -4, 6, 44, -7, 8, -1, -10]
Output: [2, 6, 44, 8, -4, -7, -1, -10]
50). Python program to move all zero digits to the end of a given list of numbers.
Input: [3, 4, 0, 0, 0, 0, 6, 0, 4, 0, 22, 0, 0, 3, 21, 0]
Output: [3, 4, 6, 4, 22, 3, 21, 0, 0, 0, 0, 0, 0, 0, 0]
51). Python program to find the list in a list of lists whose sum of elements is the highest.
Input: [[11, 2, 3], [4, 15, 6], [10, 11, 12], [7 8, 19]]
Output: [7, 8, 19]
52). Python program to find the items that start with a specific character from a given list.
Input: [‘abbcd’, ‘ppq, ‘abdd’, ‘agr’, ‘bhr’, ‘sqqa’, tools, ‘bgr’]
 
# item starts with a from the given list.
[‘abbcd’, ‘abdd’, ‘agr’]
 
# item starts with b from the given list.
[‘bhr’, ‘bgr’]
 
# item starts with c from the given list.
[]
53). Python program to count empty dictionaries from the given list.
Input: [{}, {‘a’: ‘sqatools’}, [], {}, {‘a’: 123}, {},{},()]
empty_count: 3
54). Python program to remove consecutive duplicates of given lists.
Input: [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 4]
55). Python program to pack consecutive duplicates of given list elements into sublists.
Input: [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9]
Output: [[0, 0], [1], [2], [3], [4, 4], [5], [6, 6], [7], [8, 8], [9]]
56). Python program to split a given list into two parts where the length of the first part of the list is given.
Input: [4, 6, 7, 3, 2, 5, 6, 7, 6, 4]
length of the first part is 4
Output: [[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]
57). Python program to insert items at a specific position in the list.
Input: [2, 4, 6, 8, 3, 22]
Index: 3
Item: 55
Output: [2, 4, 6, 55, 8, 3, 22]
58). Python program to select random numbers from the list.
Input: [1, 4, 5, 7, 3, 2, 9]
Selected 4 random numbers from the list.
59). Python program to create a 3*3 grid with numbers.
Output: [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
60). Python program to zip two lists of lists into a list.
list1: [[1, 3], [5, 7], [9, 11]]
list2: [[2, 4], [6, 8], [10, 12, 14]]
61). Python program to convert the first and last letter of each item from Upper case and lowercase.
Input: [‘Learn’, ‘python’, ‘From’, ‘Sqa’, tools]
Output =
[‘LearN ‘, ‘PythoN ‘, ‘FroM ‘, ‘SqA ‘, ‘ToolS ‘]
[‘learn ‘, ‘python ‘, ‘from ‘, ‘sqa ‘, ‘tools ‘]
62). Python to find maximum and minimum values in the given heterogeneous list.
Input: [‘Sqa’, 6, 5, 2, ‘Tools’]
Output: [6,2]
63). Python program to sort a given list in ascending order according to the sum of its sublist.
Input: [[3, 5, 6], [2, 1, 3], [5, 1, 1], [1, 2, 1], [0, 4, 1]]
            14         6         7           4          5
Output = [[1, 2, 1], [0, 4, 1], [2, 1, 3], [5, 1, 1], [3, 5, 6]]
64). Python program to extract the specified sizes of strings from a given list of string values.
Input: [‘Python’, ‘Sqatools’, ‘Practice’, ‘Program’, ‘test’, ‘list’]
size = 8
Output: [‘Sqatools’, ‘Practice’]
65). Python program to find the difference between consecutive numbers in a given list.
Input list: [1, 1, 3, 4, 4, 5, 6, 7]
Output list: [0, 2, 1, 0, 1, 1, 1]
66). Python program to calculate the average of the given list.
Input : [3, 5, 7, 2, 6, 12, 3]
Output: 5.428571428571429
67). Python program to count integers in a given mixed list.
Input list: [‘Hello’, 45, ‘sqa’,  23, 5, ‘Tools’, 20]
Output: 4
68). Python program to access multiple elements of the specified index from a given list.
Input list: [2, 3, 4, 7, 8, 1, 5, 6, 2, 1, 8, 2]
Index list: [0, 3, 5, 6]
Output: [2, 7, 1, 5]
69). Python program to check whether a specified list is sorted or not.
Input list : [1, 2, 3, 5, 7, 8, 9]
Output: True
 
Input list: [3, 5, 1, 6, 8, 2, 4]
Output: False
70). Python program to remove duplicate dictionaries from a given list.
Input : [{‘name’: ‘john’}, {‘city’: ‘mumbai’}, {‘Python’: ‘laguage’}, {‘name’: ‘john’}]
Output: [{‘city’: ‘mumbai’}, {‘Python’: ‘laguage’}]
71). Python program to check if the elements of a given list are unique or not.
Input: [2, 5, 6, 7, 4, 11, 2, 4, 66, 21, 22, 3]
Output: False
72). Python program to remove duplicate sublists from the list.
Input: [[1, 2], [3, 5], [1, 2], [6, 7]]
Output: [[3, 5],[6, 7]]
73). Python program to create a list by taking an alternate item from the list.
Input: [3, 5, 7, 8, 2, 9, 3, 5, 11]
Output: [3, 7, 2, 3, 11]
74). Python program to remove duplicate tuples from the list.
Input: [(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]
Output: [(2,3), (4, 6), (5,1), (7, 9)]
75). Python program to insert an element before each element of a list.
Input :[3, 5, 7, 8]
element = ‘a’
Output: [‘a’, 3, ‘a’, 5, ‘a’, 7, ‘a’, 8]
76). Python program to remove the duplicate string from the list.
Input: [‘python’, ‘is’, ‘a’, ‘best’, ‘language’, ‘python’, ‘best’]
Output: [‘python’, ‘is’, ‘a’, ‘best’, ‘language’]

77). Python program to get the factorial of each item in the list.

78). Python program to get a list of Fibonacci numbers from 1 to 20.

79). Python program to reverse all the numbers in a given list.
Input : [123, 145, 633, 654, 254]
Output: [321, 541, 336, 456, 452]
80). Python program to get palindrome numbers from a given list.
Input : [121, 134, 354, 383, 892, 232]
Output : [121, 282, 232]
81). Python program to get a count of vowels in the given list.
Input : [‘Learning’, ‘Python’, ‘From’, ‘SqaTool’]
Output : 8
82). Python program to get the list of prime numbers in a given list.
Input : [11, 8, 7, 19, 6, 29]
Output : [11,  7,  19, 29]
83). Python program to get a list with n elements removed from the left and right.
Input : [2, 5, 7, 9, 3, 4]
Remove 1 element from left
[5, 7, 9, 3, 4]
 
Remove 1 element from the right
[2, 5, 7, 9, 3]
 
Remove 2 elements from left
[7, 9, 3, 4]
 
Remove 2 elements from right
[2, 5, 7, 9]
84). Python program to create a dictionary with two lists.
Input :
list1 : [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
list2 : [234, 123, 456, 343, 223]
Output: {‘a’: 234, ‘b’: 123, ‘c’: 456, ‘d’: 343, ‘e’: 223}
85). Python program to remove the duplicate item from the list using set.
Input : [2, 5, 7, 8, 2, 3, 4, 12, 5, 6]
Output : [2, 5, 7, 8, 3, 4, 12, 6]
86). Python program to insert a sublist into the list at a specific index.
Input : [4, 6, 8, 2, 3, 5]
sublist, index
[5, 2, 6], 3
Output: [4, 6, 8, [5, 2, 6], 2, 3, 5]
87). Python program to calculate the bill per fruit purchased from a given fruits list.
Input =
Fruit list with Price: [[‘apple’, 30], [‘mango’, 50], [‘banana’, 20], [‘lichi’, 50]]
Fruit with quantity: [[‘apple’, 2]]
Output =
Fruit: Apple
Bill: 60
Fruit: mango
Bill: 500
88). Python program to calculate percentage from a given mark list, the max mark for each item is 100.
Marks_list : [80, 50, 70, 90, 95]
Output: 77%
89). Python program to get the list of all palindrome strings from the given list.
Input: [‘data’, ‘python’, ‘oko’, ‘test’, ‘ete’]
Output: [‘oko’, ‘ete’]
90). Python program to flatten a given nested list structure.
Input: [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
Output: [0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]
91). Python program to convert tuples in the list into a sublist.
Input: [(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]
Output: [[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]
92). Python program to create a dictionary from a sublist in a given list.
Input: [[‘a’, 5], [‘b’, 8], [‘c’, 11], [‘d’, 14], [‘e’, 23]]
Output: {‘a’: 5, ‘b’: 8, ‘c’: 11, ‘d’: 14, ‘e’: 23}
93). Python program to replace ‘Java’ with ‘Python’ from the given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Python’, ‘Language’]
Output: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Java’, ‘Its’, ‘Java’, ‘Language’]
94). Python program to convert the 3rd character of each word to a capital case from the given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Python’, ‘Language’]
Output: [‘HelLo’, ‘stuDent’, ‘are’, ‘leaRning’, ‘PytHon’, ‘Its’, ‘PytHon’, ‘LanGuage’]
95). Python program to remove the 2nd character of each word from a given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Python’, ‘Language’]
Output[‘Hllo’, ‘sudent’, ‘ae’, ‘larning’, ‘Pthon’, ‘Is’, ‘Pthon’, ‘Lnguage’]
96). Python program to get a length of each word and add it as a dictionary from the given list.
Input: [‘Hello’, ‘student’, ‘are’, ‘learning’, ‘Python’, ‘Its’, ‘Language’]
Output: [{‘Hello’:5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Language’: 8}]
97). Python program to remove duplicate dictionaries from the given list.
Input: [{‘Hello’:5}, {‘student’: 7}, {‘are’: 3}, {‘learning’: 8}, {‘Hello’:5}, ,{‘Language’: 8}, {‘are’: 3}]
Output: [{‘Hello’:5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Language’: 8}]
98). Python program to decode a run-length encoded given list.
Input: [[2, 1], 2, 3, [2, 4], 5, 1]
Output: [1, 1, 2, 3, 4, 4, 5, 1]
99). Python program to round every number in a given list of numbers and print the total sum of the list.
Input: [22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
Output: 27
 
list1 = [2, 4, 6, 7, 5, 8, 3, 11]

result = [x**2 for x in list1 if x%2 == 0]

print("Result :", result)

Output :

Result : [4, 16, 36, 64]

100).  Python Program to get the Median of all the elements from the list.
list1 = [2, 4, 6, 7, 5, 8, 3, 11]

result = [x**2 for x in list1 if x%2 == 0]

print("Result :", result)

Output :

Result : [4, 16, 36, 64]

101). Python Program to get the Standard deviation of the list element.
102). Python program to convert all numbers to binary format from a given list.
103). Python program to convert all the numbers into Roman numbers from the given list.

Python Lists Project Ideas

1). To-do List: Python list program that allows users to create and manage their to-do lists. Users can add, delete, and update tasks on their list.  Each task can add along with a unique id which will first element of each sub-list through which the user can easily identify the item and task.

2). Shopping List: A program that allows users to create and manage their shopping list. Users can add, delete, and update items on their list. There are many Python list programs listed above those can help user to solve this project.

3). Game Leaderboard: A program that keeps track of scores for multiple players in a game. Use Python lists to store the scores and display a leaderboard of the top players. Nowadays people like to play games and users can apply a basic understanding of Python list programs to write code for this mini-project. 

4). Student Gradebook: A Python list program that allows teachers to enter and manage student grades. Use Python lists and sub-lists to store student names and their corresponding grades.

5). Password Manager: A program that securely stores and manages passwords using Python lists. Users can add, delete, and update passwords, and the program can encrypt the data for added security, Its good exercises to apply understanding Python list programs. 

6). Music Library: A Python list program that allows users to create and manage a music library. Users can add, delete, and update songs, artists, and album details in the list.

7). Recipe Book: A program that allows users to create and manage their recipe book. Users can add, delete, and update recipes, ingredients, and cooking instructions.

8). Contact List: A Python list program that allows users to manage their contact list. Users can add, delete, and update contacts’ names, phone numbers, and email addresses in the list.

9). Movie Library: A Python list program that allows users to create and manage a movie library. Users can add, delete, and update movies, actors, and directors in the list.

10). Sports Team Roster: A program that allows coaches to manage their sports team roster. Coaches can add, delete, and update players’ names, positions, and statistics in the list.

Official reference for python list data structure