Python Practice › List Programs
Python List Programs with Solutions
100 list programs, from sums and reversing to sublists, de-duplication and run-length decoding. Lists are Python’s most used data structure, and these exercises appear in most Python interviews. Every solution is explained and shows its real output.
Before you start
Read the Lists and List Comprehension lesson first if the topic is new to you.
All programs
- #1Square of each number in a list
Write a Python program to calculate the square of each number in a list.Easy - #2Combine two lists
Write a Python program to combine two lists.Easy - #3Sum of all elements
Write a Python program to calculate the sum of all elements in a list.Easy - #4Product of all elements
Write a Python program to find the product of all elements in a list.Easy - #5Minimum and maximum
Write a Python program to find the smallest and largest elements of a list.Easy - #6Separate even and odd numbers
Write a Python program to separate the even and odd numbers of a list.Easy - #7Remove duplicate elements
Write a Python program to remove every duplicate value from a list, keeping the first occurrence of each value in its original position.Medium - #8Pairs whose sum is 10
Write a Python program to print every pair of elements from a list whose sum is 10.Medium - #9Squares of even numbers
Write a Python program to print the squares of all even numbers in a list.Easy - #10Odd values left, even values right
Write a Python program to rearrange a list so all odd values come first and all even values after them, keeping their order.Easy - #11Common elements of two lists
Write a Python program to get the common elements of two lists, in the order of the first list.Easy - #12Reverse a list with a for loop
Write a Python program to reverse a list using a for loop.Easy - #13Reverse a list with a while loop
Write a Python program to reverse a list using a while loop.Easy - #14Reverse a list with slicing
Write a Python program to reverse a list using slicing.Easy - #15reversed() and reverse()
Write a Python program to reverse a list withreversed()and withreverse().Easy - #16Copy a list
Write a Python program to copy a list so that changing the copy does not change the original.Easy - #17Do two lists share a member?
Write a Python program that returns True if two lists have at least one common member.Easy - #18Remove the 1st, 3rd and 6th elements
Write a Python program to remove the 1st, 3rd and 6th elements of a list.Easy - #19Remove negative values
Write a Python program to remove negative values from a list.Easy - #20Elements divisible by 3 and 7
Write a Python program to get all elements divisible by both 3 and 7.Easy - #21Is a list a palindrome?
Write a Python program to check whether a list reads the same forwards and backwards.Easy - #22Words that contain vowels
Write a Python program to get the words of a string that contain at least one vowel.Easy - #23Add two lists with extend()
Write a Python program to add one list to another withextend().Easy - #24sort() vs sorted()
Write a Python program to sort a list withsort()and withsorted().Easy - #25Remove by index with pop()
Write a Python program to remove an element at a given index withpop().Easy - #26max, min and sum
Write a Python program to get the maximum, minimum and sum of a list with built-in functions.Easy - #27Does a list contain a sublist?
Write a Python program to check whether a list contains another list as a consecutive run.Medium - #28Sublists with 5 or more elements
Write a Python program to generate all consecutive sublists with 5 or more elements.Medium - #29Second largest number
Write a Python program to find the second largest number in a list (ignoring duplicates).Easy - #30Second smallest number
Write a Python program to find the second smallest number in a list.Easy - #31Join elements with a special character
Write a Python program to merge all elements of a list into one string using a special character.Easy - #32Difference between two lists
Write a Python program to get the items in the first list that are not in the second.Easy - #33Reverse each string in a list
Write a Python program to reverse each string in a list.Easy - #34Pair up two lists as sublists
Write a Python program to combine items at the same position of two lists into sublists.Easy - #35Keys and values from a list of dicts
Write a Python program to get all keys and all values from a list of single-item dictionaries.Easy - #36Numbers that appear exactly once
Write a Python program to get the numbers that appear exactly once in a list.Easy - #37Convert a string to a list
Write a Python program to convert a string into a list of words and a list of characters.Easy - #38Replace the first and last items
Write a Python program to replace the first and last numbers of a list with “SQA” and “Tools”.Easy - #39Check if an element exists
Write a Python program to check whether an element exists in a list.Easy - #40Remove elements at odd indexes
Write a Python program to remove all elements at odd indexes.Easy - #41At least one common member
Write a Python program that returns True if two lists have at least one common member.Easy - #42Join numbers into one number
Write a Python program to convert a list of numbers into a single number.Easy - #43Join words without spaces
Write a Python program to join a list of words into one string.Easy - #44Print each element separately
Write a Python program to print each element of a list of tuples separately.Easy - #45Numbers and their squares as sublists
Write a Python program to create sublists of each number from 1 to 10 and its square.Easy - #46Groups of five consecutive numbers
Write a Python program to create lists of five consecutive numbers from 1 to 20.Easy - #47Add a prefix to every item
Write a Python program to insert a string at the beginning of every item.Easy - #48Iterate two lists at once
Write a Python program to iterate over two lists simultaneously and build a list of pairs.Easy - #49Positives left, negatives right
Write a Python program to move all positive numbers to the left and negative numbers to the right, keeping their order.Easy - #50Move zeros to the end
Write a Python program to move all zeros to the end of a list.Easy - #51Sublist with the highest sum
Write a Python program to find the sublist whose elements add up to the highest total.Easy - #52Items starting with a letter
Write a Python program to find the items that start with a given character.Easy - #53Count empty dictionaries
Write a Python program to count the empty dictionaries in a list (ignoring other empty containers).Easy - #54Remove consecutive duplicates
Write a Python program to remove consecutive duplicates from a list.Easy - #55Pack consecutive duplicates
Write a Python program to pack consecutive duplicates into sublists.Easy - #56Split a list at a given length
Write a Python program to split a list into two parts where the first part has a given length.Easy - #57Insert an item at an index
Write a Python program to insert an item at a specific position.Easy - #58Pick random numbers from a list
Write a Python program to select 4 random numbers from a list.Easy - #593 × 3 grid
Write a Python program to create a 3 × 3 grid where every row is [4, 5, 6].Easy - #60Zip two lists of lists
Write a Python program to join two lists of lists item by item.Easy - #61Capitalise the first and last letters
Write a Python program to make the first and last letters of each word uppercase, and also print every word in lowercase.Easy - #62Max and min of a mixed list
Write a Python program to find the maximum and minimum numbers in a list that also contains strings.Easy - #63Sort sublists by their sum
Write a Python program to sort a list of sublists by the sum of each sublist.Easy - #64Strings of a given length
Write a Python program to extract the strings of a given length.Easy - #65Differences between neighbours
Write a Python program to find the difference between consecutive numbers.Easy - #66Average of a list
Write a Python program to calculate the average of a list.Easy - #67Count integers in a mixed list
Write a Python program to count the integers in a mixed list.Easy - #68Elements at several indexes
Write a Python program to get the elements at a list of indexes.Easy - #69Is a list sorted?
Write a Python program to check whether a list is sorted in ascending order.Easy - #70Remove duplicate dictionaries
Write a Python program to remove duplicate dictionaries from a list, keeping the first of each.Easy - #71Are all elements unique?
Write a Python program to check whether all elements of a list are unique.Easy - #72Keep sublists that are not repeated
Write a Python program to keep only the sublists that appear once.Easy - #73Every other item
Write a Python program to create a list from every second item.Easy - #74Remove duplicate tuples
Write a Python program to remove duplicate tuples from a list.Easy - #75Insert an element before each item
Write a Python program to insert an element before each element of a list.Easy - #76Remove duplicate strings
Write a Python program to remove duplicate strings from a list.Easy - #77Factorial of each item
Write a Python program to get the factorial of each item in a list.Easy - #78Fibonacci numbers from 1 to 20
Write a Python program to get the Fibonacci numbers between 1 and 20.Easy - #79Reverse every number in a list
Write a Python program to reverse the digits of every number in a list.Easy - #80Palindrome numbers in a list
Write a Python program to get the palindrome numbers from a list.Easy - #81Count vowels in a list of words
Write a Python program to count the vowels in a list of words.Easy - #82Prime numbers in a list
Write a Python program to get the prime numbers from a list.Easy - #83Remove n elements from the left or right
Write a Python program to remove n elements from the left and from the right of a list.Easy - #84Dictionary from two lists
Write a Python program to create a dictionary from a list of keys and a list of values.Easy - #85Remove duplicates with a set
Write a Python program to remove duplicates from a list using a set.Easy - #86Insert a sublist at an index
Write a Python program to insert a sublist into a list at a given index.Easy - #87Bill per fruit purchased
Write a Python program to calculate the bill for each fruit purchased, from a price list and a quantity list.Medium - #88Percentage from marks
Write a Python program to calculate the percentage from a list of marks out of 100 each.Easy - #89Palindrome strings in a list
Write a Python program to get the palindrome strings from a list.Easy - #90Flatten a nested list
Write a Python program to flatten a nested list.Medium - #91Tuples to sublists
Write a Python program to convert the tuples in a list into lists.Easy - #92Dictionary from sublists
Write a Python program to create a dictionary from [key, value] sublists.Easy - #93Replace a word in a list
Write a Python program to replace “Java” with “Python” in a list of words.Easy - #94Capitalise the 3rd character of each word
Write a Python program to make the 3rd character of each word uppercase.Easy - #95Remove the 2nd character of each word
Write a Python program to remove the 2nd character of each word.Easy - #96Word lengths as a dictionary
Write a Python program to build a dictionary of each word and its length.Easy - #97Remove repeated dictionaries
Write a Python program to remove repeated dictionaries from a list.Easy - #98Decode a run-length encoded list
Write a Python program to decode a run-length encoded list, where [n, x] means x repeated n times.Medium - #99Round every number and sum
Write a Python program to round every number in a list and print the sum of the rounded numbers multiplied by the list length.Easy - #100Median of a list
Write a Python program to get the median of all the elements of a list.Easy