List Programs
sum, sort, slice, duplicates. Most interview warm-ups are this file.
25 programs · open one, then the next
The matching chapter is Day 5: Lists and List Methods in the Python course. For a short check, try the Python quiz (300 questions). Every topic is listed on Python exercises.
Start with Sum of List Elements- 01Sum of List ElementsBeginnerCalculate the sum of all elements in a list of numbers.Open example →py# Program to find sum of list elements numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 02Largest Element in ListBeginnerFind the largest element in a list of numbers.Open example →py# Program to find largest element in a list numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 03Smallest Element in ListBeginnerFind the smallest element in a list of numbers.Open example →py# Program to find smallest element in a list numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 04Count Occurrences in ListBeginnerCount how many times a given element occurs in a list.Open example →py# Program to count occurrences of an element in a list items = input("Enter list elements separated by space: ").split() target = input("Enter element to count: ")
- 05Remove Duplicates from ListBeginnerRemove duplicate elements from a list while preserving order.Open example →py# Program to remove duplicates from a list while preserving order items = input("Enter list elements separated by space: ").split()
- 06Merge Two ListsBeginnerMerge two lists into a single list.Open example →py# Program to merge two lists list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()
- 07Intersection of ListsBeginnerFind the common elements between two lists.Open example →py# Program to find intersection of two lists list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()
- 08Union of ListsBeginnerFind the union of two lists (unique elements from both).Open example →py# Program to find union of two lists list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()
- 09Sort ListBeginnerSort a list of numbers in ascending order.Open example →py# Program to sort a list of numbers numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 10Reverse ListBeginnerReverse a list in-place using slicing.Open example →py# Program to reverse a list items = input("Enter list elements separated by space: ").split()
- 11Check if List is SortedBeginnerCheck whether a list is sorted in non-decreasing order.Open example →py# Program to check if a list is sorted numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 12Find Second Largest ElementBeginnerFind the second largest distinct element in a list.Open example →py# Program to find second largest distinct element in a list numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 13Find N Largest ElementsBeginnerFind the N largest elements in a list.Open example →py# Program to find N largest elements in a list numbers = list(map(float, input("Enter numbers separated by space: ").split())) N = int(input("Enter N: "))
- 14Clone / Copy ListBeginneritems[:] and list(items) copy the list, not the name. Change one, the other stays.Open example →py# Program to clone a list items = input("Enter list elements separated by space: ").split()
- 15Rotate ListBeginnerRotate a list by K positions to the right.Open example →py# Program to rotate a list by K positions items = input("Enter list elements separated by space: ").split() K = int(input("Enter rotation step K: "))
- 16Remove Empty ListsBeginnerRemove empty sublists from a list of lists.Open example →py# Program to remove empty lists from a list items = eval(input("Enter a list of lists (e.g., [[], [1], [], [2, 3]]): "))
- 17Flatten Nested ListIntermediateFlatten a shallow nested list (list of lists) into a single list.Open example →py# Program to flatten a nested list (one level) items = eval(input("Enter a nested list (e.g., [[1, 2], [3, 4]]): "))
- 18Find Common Elements of Two ListsBeginnerFind elements that appear in both lists (preserving list order of the first).Open example →py# Program to find common elements of two lists list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()
- 19List Slicing ProgramsBeginner[:N] prefix, [-N:] suffix, [::K] every Kth. K=0 is a crash, so the code bails.Open example →py# Program to demonstrate list slicing items = input("Enter list elements separated by space: ").split()
- 20Replace List ElementsBeginnerReplace all occurrences of a value in a list with another value.Open example →py# Program to replace list elements items = input("Enter list elements separated by space: ").split() old = input("Enter value to replace: ")
- 21Remove Element by ValueBeginnerRemove the first occurrence of a value from a list, if present.Open example →py# Program to remove first occurrence of a value from list items = input("Enter list elements separated by space: ").split() value = input("Enter value to remove: ")
- 22Count Prime Numbers in ListIntermediateCount how many prime numbers are present in a list of integers.Open example →py# Program to count prime numbers in a list def is_prime(n: int) -> bool: if n < 2:
- 23Pair Sum ProblemIntermediateCheck if there exists a pair of numbers in the list that adds up to a target sum.Open example →py# Program to check if any pair in list sums to target numbers = list(map(int, input("Enter integers separated by space: ").split())) target = int(input("Enter target sum: "))
- 24Unique Elements from Both ListsIntermediateFind elements that appear in exactly one of the two lists (symmetric difference).Open example →py# Program to find elements unique to each of two lists list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()
- 25List DifferenceBeginnerFind elements that are in the first list but not in the second.Open example →py# Program to find list difference (A - B) list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()