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 ElementsBeginnerRead a row of Python numbers, convert each token, and sum the list. New programmers follow split, map, and float while handling blank or malformed responses.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 a Python list maximum without sorting the input. A beginner numeric example explains empty-list protection, repeated maxima, and all-negative collections.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 ListBeginnerPick the least Python numeric value without changing list order. Beginners follow parsing, guard empty input, and avoid comparing number-like tokens as strings.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 complete Python list items rather than substrings inside them. A beginner list.count exercise explains repeated matches, missing targets, and text equality.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 ListBeginnerKeep first appearances while removing Python list duplicates. Beginners give a set the lookup job and a result list the ordering job, without sorting the input.Open example →py# Program to remove duplicates from a list while preserving order items = input("Enter list elements separated by space: ").split()
- 06Merge Two ListsBeginnerJoin two Python lists end to end with + and keep every occurrence. New programmers distinguish a new outer list from mutation, deduplication, or a sorted merge.Open example →py# Program to merge two lists list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()
- 07Intersection of ListsBeginnerKeep values shared by two Python lists with set &. Beginner membership practice removes repeated copies and explains why the printed result order can change.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 ListsBeginnerCombine distinct Python values from either list with set |. A beginner contrast with concatenation explains lost duplicates and the lack of a guaranteed order.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 Python numbers in place without assigning the None result. Beginners compare list.sort with sorted and see how conversion prevents text-order mistakes.Open example →py# Program to sort a list of numbers numbers = list(map(float, input("Enter numbers separated by space: ").split()))
- 10Reverse ListBeginnerReverse Python list positions with a slice instead of sorting values. Beginner examples retain the source order and explain why the new list is a shallow copy.Open example →py# Program to reverse a list items = input("Enter list elements separated by space: ").split()
- 11Check if List is SortedBeginnerTest Python non-decreasing order by comparing with a sorted copy. Beginners allow equal neighbors, include empty lists, and keep the original sequence untouched.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 ElementBeginnerRepeated maxima should not fill both ranks in a distinct Python runner-up task. Beginners combine set, sorted, a length check, and negative indexing safely.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 ElementsBeginnerTake a prefix of a descending Python sorted list to select N largest entries. Beginners retain duplicates and handle requests longer than the available input.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 ListBeginnerAssignment is not a Python list copy. Compare full slicing with list construction in a beginner example that explains aliasing and shared nested mutable objects.Open example →py# Program to clone a list items = input("Enter list elements separated by space: ").split()
- 15Rotate ListBeginnerMove the final K Python list items to the front with two slices. Beginners wrap large steps with modulo and test zero, negative steps, and an empty collection.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 ListsBeginnerFilter empty Python sublists with truthiness rather than manual deletion. Beginners parse literal data without eval and learn why [0] still belongs in the result.Open example →pyimport ast # Program to remove empty lists from a list
- 17Flatten Nested ListIntermediateRead a Python nested comprehension in outer-then-inner order. A beginner flattening example parses literal data and removes one nesting level, not every level.Open example →pyimport ast # Program to flatten a nested list (one level)
- 18Find Common Elements of Two ListsBeginnerPreserve Python first-list order while keeping entries also present in a second list. Beginners compare duplicate-preserving filtering with set intersection.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 ProgramsBeginnerTry first-N, last-N, and stepped Python list slices in one beginner exercise. See exclusive stops, safe oversized bounds, and zero-count and zero-step traps.Open example →py# Program to demonstrate list slicing items = input("Enter list elements separated by space: ").split()
- 20Replace List ElementsBeginnerChoose a replacement for each matching Python list entry with a conditional expression. Beginners keep nonmatches, list length, and the original list.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 ValueBeginnerDelete one matching Python list entry, not every duplicate, with remove. Beginners check membership first and avoid assigning the method’s None return value.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 ListIntermediateReuse a Python primality helper to count list occurrences. Beginners reject values below two, stop at a divisor, and keep repeated primes in the final total.Open example →py# Program to count prime numbers in a list def is_prime(n: int) -> bool: if n < 2:
- 23Pair Sum ProblemIntermediateFind a Python pair sum by remembering earlier values in a set. Beginners check the missing partner before insertion so one entry cannot pair with itself.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 ListsIntermediateKeep Python values belonging to exactly one list with set ^. Beginners distinguish symmetric difference from union and one-way filtering without assuming order.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 DifferenceBeginnerFilter Python first-list entries that never appear in the second list. A beginner directional-difference exercise preserves unmatched repeats and input order.Open example →py# Program to find list difference (A - B) list1 = input("Enter first list elements: ").split() list2 = input("Enter second list elements: ").split()