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 by AmitDiwan
Page 133 of 840
Append 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 MorePython program to remove duplicate elements from a Circular Linked List
When it is required to remove duplicates from a circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have NULL value in the last node. Another class needs to be created that would have an initialization function, and the head of the node would be initialized to ...
Read MorePython program to insert a new node at the middle of the Circular Linked List
When it is required to insert a new node at the middle of the circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node. Instead, the last node points back to the first node. Node Class Structure The Node class ...
Read MorePython program to insert a new node at the end of the Circular Linked List
When it is required to insert a new node at the end of the circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the tail are adjacent to each other. They are connected to form a circle, and don't have NULL value in the last node. Instead, the last node points back to the first node. ...
Read More