Programming Articles

Page 401 of 2547

Python Program to Display the Nodes of a Linked List in Reverse without using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 263 Views

When it is required to display the nodes of a linked list in reverse without using the method of recursion, we can use an iterative approach. This involves finding the last unprinted node in each iteration and displaying it. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): ...

Read More

Python Program to Display all the Nodes in a Linked List using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 667 Views

When it is required to display the elements/nodes in a linked list using recursion, we need a method to add values to the linked list and a recursive helper method that calls itself repeatedly to print the values. The recursive approach provides an elegant way to traverse the entire linked list. Below is a demonstration for the same − Node and LinkedList Classes First, we define the basic structure of our linked list ? class Node: def __init__(self, data): self.data = data ...

Read More

Python Program to Search for an Element in the Linked List without using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 575 Views

Searching for an element in a linked list is a fundamental operation. This article demonstrates how to implement a non-recursive search algorithm that finds an element and returns its index position in a linked list. Node Class Definition First, we create a Node class to represent individual elements in the linked list ? class Node: def __init__(self, data): self.data = data self.next = None Linked List Implementation The linked list class contains methods ...

Read More

Python Program to Create a Linked List & Display the Elements in the List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a reference to the next node. Unlike arrays, linked list elements are not stored in contiguous memory locations. Below is a demonstration of creating a linked list and displaying its elements ? Node Class Structure First, we create a Node class to represent individual elements ? class Node: def __init__(self, data): self.data = data self.next ...

Read More

Append Dictionary Keys and Values (In order ) in dictionary using Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 866 Views

When you need to append dictionary keys and values in order, Python provides several approaches. The most straightforward method uses the list() function with .keys() and .values() methods to extract and concatenate them. Basic Approach Using list() and Concatenation This method converts dictionary keys and values to lists, then concatenates them ? my_dict = {"January": 1, "Feb": 2, "March": 3, "April": 4, "May": 5, "June": 6} print("The dictionary is:") print(my_dict) my_result = list(my_dict.keys()) + list(my_dict.values()) print("The ordered key and value are:") print(my_result) The dictionary is: {'January': 1, 'Feb': 2, 'March': ...

Read More

Convert key-values list to flat dictionary in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 676 Views

When working with dictionaries that contain lists as values, you may need to convert them into a flat dictionary where elements from the lists become key-value pairs. Python provides several approaches to achieve this transformation. Understanding the Problem A key-values list dictionary has keys mapped to lists of values. Converting to a flat dictionary means pairing corresponding elements from these lists into key-value pairs. Using zip() Method The most common approach uses zip() to pair corresponding elements from two lists ? my_dict = {'month_num': [1, 2, 3, 4, 5, 6], 'name_of_month': ['Jan', 'Feb', 'March', ...

Read More

Extract tuples having K digit elements in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 533 Views

When working with lists of tuples, you may need to extract tuples containing elements with a specific number of digits. This can be accomplished using list comprehension with the all() function and len() to check digit counts. Example Here's how to extract tuples where all elements have exactly K digits ? my_list = [(34, 56), (45, 6), (111, 90), (11, 35), (78, )] print("The list is :") print(my_list) K = 2 print("The value of K has been initialized to", str(K)) my_result = [sub for sub in my_list if all(len(str(elem)) == K for elem ...

Read More

Python Program to Take in a String and Replace Every Blank Space with Hyphen

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When it is required to take a string and replace every blank space with a hyphen, the replace() method can be used. It takes two parameters: the blank space, and the value with which it needs to be replaced (hyphen in this case). Below is a demonstration of the same − Using replace() Method text = "Hello world Python programming" print("Original string:") print(text) modified_text = text.replace(' ', '-') print("Modified string:") print(modified_text) Original string: Hello world Python programming Modified string: Hello-world-Python-programming Interactive Example Here's an example that takes user input ...

Read More

Python Program to Replace all Occurrences of 'a' with $ in a String

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 843 Views

When it is required to replace all the occurrences of 'a' with a character such as '$' in a string, Python provides multiple approaches. You can iterate through the string manually, use the built-in replace() method, or use string translation methods. Method 1: Using Manual Iteration This approach iterates through each character and builds a new string ? my_str = "Jane Will Rob Harry Fanch Dave Nancy" changed_str = '' for char in range(0, len(my_str)): if(my_str[char] == 'a'): changed_str += '$' ...

Read More

Python Program to Find Element Occurring Odd Number of Times in a List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When it is required to find an element that occurs odd number of times in a list, several approaches can be used. The most common methods include nested loops to count occurrences, using Python's Counter from collections module, or using XOR operations for optimization. Method 1: Using Nested Loops This method iterates through the list and counts occurrences of each element using nested loops ? def odd_occurrence(my_list, list_size): for i in range(0, list_size): count = 0 ...

Read More
Showing 4001–4010 of 25,466 articles
« Prev 1 399 400 401 402 403 2547 Next »
Advertisements