1. List Comprehension - SquaresProblem StatementCreate a list of squares of numbers from 1 to 10 using list comprehension.ApproachList comprehension syntax: [expression for item in iterable]. Use range(1, 11) and square each number.Solutionsquares = [x**2 for x in range(1, 11)] print(squares)
2. Filter Even NumbersProblem StatementGiven a list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], create a new list containing only the even numbers using list comprehension.ApproachAdd a condition to the list comprehension: [x for x in list if condition].Solutionnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evens = [x for x in numbers if x % 2 == 0] print(evens)
3. List SlicingProblem StatementGiven the list [10, 20, 30, 40, 50, 60, 70], extract elements from index 2 to 5 (inclusive of 2, exclusive of 6).ApproachUse slicing syntax: list[start:end]. Remember that end is exclusive.Solutionnumbers = [10, 20, 30, 40, 50, 60, 70] result = numbers[2:6] print(result)
4. Reverse a ListProblem StatementReverse the list [1, 2, 3, 4, 5] and print it.ApproachYou can use the reverse() method or slicing [::-1].Solutionnumbers = [1, 2, 3, 4, 5] numbers.reverse() print(numbers) # Or: print(numbers[::-1])
5. Find Maximum in ListProblem StatementFind and print the maximum value in the list [45, 23, 67, 89, 12, 56].ApproachUse the max() built-in function.Solutionnumbers = [45, 23, 67, 89, 12, 56] print(max(numbers))
6. Remove DuplicatesProblem StatementGiven the list [1, 2, 2, 3, 4, 4, 5], remove duplicates and print the unique values.ApproachConvert the list to a set to remove duplicates, then convert back to a list if needed.Solutionnumbers = [1, 2, 2, 3, 4, 4, 5] unique = list(set(numbers)) print(unique)
7. Dictionary IterationProblem Statement`Given the dictionary {"a": 1, "b": 2, "c": 3ApproachUse the items() method to iterate over key-value pairs.Solution`data = {"a": 1, "b": 2, "c": 3
8. Merge Two DictionariesProblem Statement`Merge two dictionaries: dict1 = {"a": 1, "b": 2ApproachUse the update() method or the ** unpacking operator (Python 3.5+).Solution`dict1 = {"a": 1, "b": 2
9. Count OccurrencesProblem StatementCount the occurrences of each character in the string "hello". Store the result in a dictionary.ApproachIterate through the string and use a dictionary to count each character.Solution`text = "hello" count = {
10. Tuple UnpackingProblem StatementGiven the tuple (10, 20, 30), unpack it into three variables a, b, c and print them.ApproachTuple unpacking allows you to assign multiple variables at once.Solutiondata = (10, 20, 30) a, b, c = data print(a, b, c)
11. Set Operations - UnionProblem Statement`Find the union of two sets: set1 = {1, 2, 3ApproachUse the union() method or the | operator.Solution`set1 = {1, 2, 3
12. Set Operations - IntersectionProblem Statement`Find the intersection of two sets: set1 = {1, 2, 3, 4ApproachUse the intersection() method or the & operator.Solution`set1 = {1, 2, 3, 4
13. List of DictionariesProblem StatementCreate a list of 3 dictionaries, each representing a person with "name" and "age" keys.ApproachA list can contain dictionaries as elements.Solution`people = [ {"name": "Alice", "age": 25
14. Nested Dictionary AccessProblem Statement`Given data = {"user": {"name": "John", "age": 30ApproachUse multiple square brackets to access nested dictionary values.Solution`data = {"user": {"name": "John", "age": 30
15. Sort a ListProblem StatementSort the list [5, 2, 8, 1, 9] in ascending order.ApproachUse the sort() method or the sorted() function.Solutionnumbers = [5, 2, 8, 1, 9] numbers.sort() print(numbers)
16. List Methods - ExtendProblem StatementGiven list1 = [1, 2, 3] and list2 = [4, 5, 6], extend list1 with list2 and print the result.ApproachThe extend() method adds all elements from another list.Solutionlist1 = [1, 2, 3] list2 = [4, 5, 6] list1.extend(list2) print(list1)
17. Dictionary ComprehensionProblem StatementCreate a dictionary where keys are numbers from 1 to 5 and values are their cubes using dictionary comprehension.Approach`Dictionary comprehension syntax: {key_expr: value_expr for item in iterableSolution`cubes = {x: x**3 for x in range(1, 6)
18. Check if Key ExistsProblem StatementCheck if the key "name" exists in the dictionary { "name": "Alice", "age": 25 }.ApproachUse the 'in' keyword to check for key existence.Solution`person = {"name": "Alice", "age": 25
19. Zip Two ListsProblem StatementCombine two lists names = ["Alice", "Bob"] and ages = [25, 30] into a list of tuples.ApproachUse the zip() function to combine iterables element-wise.Solutionnames = ["Alice", "Bob"] ages = [25, 30] result = list(zip(names, ages)) print(result)
20. Enumerate a ListProblem StatementPrint each element in the list ["apple", "banana", "cherry"] along with its index.ApproachUse the enumerate() function to get both index and value.Solution`fruits = ["apple", "banana", "cherry"] for index, fruit in enumerate(fruits): print(f"{index