Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python Program to Search for an Element in the Linked List without using Recursion
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 MorePython Program to Create a Linked List & Display the Elements in the List
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 MoreAppend Dictionary Keys and Values (In order ) in dictionary using Python
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 MoreConvert key-values list to flat dictionary in Python
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 MoreExtract tuples having K digit elements in Python
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 MorePython Program to Take in a String and Replace Every Blank Space with Hyphen
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 MorePython Program to Replace all Occurrences of 'a' with $ in a String
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 MorePython Program to Find Element Occurring Odd Number of Times in a List
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 MorePython Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat
When it is required to remove a specific occurrence of a given word in a list of words, given that the words can be repeated, a method can be defined that iterates through the list and increments a counter. If the count matches the specific occurrence, then that element can be deleted from the list. Example Below is a demonstration of removing the nth occurrence of a word from a list − def remove_word(my_list, my_word, N): count = 0 for i in range(0, len(my_list)): ...
Read MoreSuperscript in Python plots
Superscript notation is essential for displaying scientific formulas and units in Python plots. Matplotlib supports LaTeX-style mathematical notation using the $\mathregular{}$ syntax to create superscripts and subscripts in titles, axis labels, and legends. Basic Superscript Syntax Use $\mathregular{text^{superscript}}$ format where the caret ^ indicates superscript and curly braces {} contain the superscript text ? import matplotlib.pyplot as plt # Simple superscript example plt.figure(figsize=(6, 4)) plt.text(0.5, 0.5, r'$\mathregular{x^2}$', fontsize=20, ha='center') plt.text(0.5, 0.3, r'$\mathregular{E=mc^2}$', fontsize=16, ha='center') plt.xlim(0, 1) plt.ylim(0, 1) plt.title('Basic Superscript Examples') plt.show() Physics Formula Plot with Superscripts Let's create a force vs ...
Read More