Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character

AmitDiwan
Updated on 25-Mar-2026 17:38:52

2K+ Views

When you need to group words by their first character, you can create a dictionary where keys are first characters and values are lists of words starting with that character. This uses the split() method, dictionary operations, and conditional logic. Example Below is a demonstration for the same − my_string = "Hey Jane how are you doing today" split_string = my_string.split() my_dict = {} for elem in split_string: if elem[0] not in my_dict.keys(): my_dict[elem[0]] = [] ... Read More

Python Program to Form a Dictionary from an Object of a Class

AmitDiwan
Updated on 25-Mar-2026 17:38:37

3K+ Views

When working with classes in Python, you often need to convert an object's attributes into a dictionary format. Python provides the __dict__ attribute that automatically creates a dictionary containing all instance attributes of an object. Basic Example Here's how to create a dictionary from a class object using the __dict__ attribute ? class BaseClass: def __init__(self): self.A = 32 self.B = 60 my_instance = BaseClass() print("An instance of the class has been created") print(my_instance.__dict__) ... Read More

Python Program to Multiply All the Items in a Dictionary

AmitDiwan
Updated on 25-Mar-2026 17:38:21

955 Views

When you need to multiply all values in a dictionary, you can iterate through the dictionary values and multiply them together. Python dictionaries store key-value pairs, and we can access the values to perform mathematical operations. Basic Approach Using a Loop The simplest method is to iterate through dictionary values and multiply them with a running product ? my_dict = {'Jane': 99, 'Will': 54, 'Mark': -3} result = 1 for value in my_dict.values(): result = result * value print("The result of multiplying all values in the dictionary is:") print(result) ... Read More

Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x).

AmitDiwan
Updated on 25-Mar-2026 17:38:04

2K+ Views

When it is required to generate a dictionary that contains numbers within a given range in a specific form, the input is taken from the user, and a simple 'for' loop is used. This creates key−value pairs where each number maps to its square. Example Below is a demonstration for the same − my_num = int(input("Enter a number.. ")) my_dict = dict() for elem in range(1, my_num + 1): my_dict[elem] = elem * elem print("The generated elements of the dictionary are : ") print(my_dict) Output Enter a number.. ... Read More

Python Program to Concatenate Two Dictionaries Into One

AmitDiwan
Updated on 25-Mar-2026 17:37:47

2K+ Views

When working with dictionaries in Python, you often need to combine multiple dictionaries into one. Python provides several methods to concatenate dictionaries, each with different behaviors and use cases. Using the update() Method The update() method modifies the original dictionary by adding key-value pairs from another dictionary ? my_dict_1 = {'J': 12, 'W': 22} my_dict_2 = {'M': 67} print("The first dictionary is:") print(my_dict_1) print("The second dictionary is:") print(my_dict_2) my_dict_1.update(my_dict_2) print("The concatenated dictionary is:") print(my_dict_1) The first dictionary is: {'J': 12, 'W': 22} The second dictionary is: {'M': 67} The concatenated dictionary ... Read More

Python Program to find the factorial of a number without recursion

AmitDiwan
Updated on 25-Mar-2026 17:37:27

6K+ Views

When it is required to find the factorial of a number without using recursion, the while loop can be used. The factorial of a number n is the product of all positive integers from 1 to n, denoted as n!. What is Factorial? Factorial of a number n (written as n!) is defined as: n! = n × (n-1) × (n-2) × ... × 2 × 1 0! = 1 (by definition) Examples: 5! = 5 × 4 × 3 × 2 × 1 = 120 Using While Loop We can calculate factorial ... Read More

Python Program to Find the Fibonacci Series without Using Recursion

AmitDiwan
Updated on 25-Mar-2026 17:37:08

5K+ Views

When it is required to find the Fibonacci series without using recursion technique, we can use iterative methods with loops. The Fibonacci sequence starts with two initial numbers, and each subsequent number is the sum of the two preceding ones. What is Fibonacci Series? The Fibonacci series is a sequence where each number is the sum of the two preceding numbers. For example: 0, 1, 1, 2, 3, 5, 8, 13, 21... Method 1: Using While Loop with User Input This approach takes custom starting numbers and term count from the user ? first_num ... Read More

Python Program to Find the Length of a List Using Recursion

AmitDiwan
Updated on 25-Mar-2026 17:36:49

2K+ Views

When it is required to find the length of a list using recursion, a user-defined method is used along with simple indexing technique. This approach demonstrates how recursion can solve problems by breaking them into smaller subproblems. A list can store heterogeneous values (i.e., data of any data type like integer, floating point, strings, and so on). Recursion computes output of small bits of the bigger problem and combines these bits to give the solution to the bigger problem. Method 1: Using Simple Recursion The most straightforward recursive approach processes one element at a time ? ... Read More

Python Program to Find the Total Sum of a Nested List Using Recursion

AmitDiwan
Updated on 25-Mar-2026 17:36:33

755 Views

When working with nested lists, we often need to calculate the sum of all elements regardless of their depth. Recursion is an elegant technique where a function calls itself to solve smaller parts of the problem until reaching a base case. Recursion breaks down complex nested structures by processing each element. If an element is a list, the function calls itself recursively. If it's a number, it adds to the total sum. Syntax def recursive_sum(nested_list): total = 0 for element in nested_list: ... Read More

Python Program to Reverse a String Using Recursion

AmitDiwan
Updated on 25-Mar-2026 17:36:15

3K+ Views

When it is required to reverse a string using recursion technique, a user-defined method is used along with recursion. The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem. How Recursion Works for String Reversal The recursive approach works by taking the first character and appending it to the reversed substring. The base case is when the string becomes empty. Example Below is a demonstration for the same − def reverse_string(my_string): if len(my_string) == 0: ... Read More

Advertisements