Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value. In this article, we will learn how to extract dictionaries that contain a specific key from a list of dictionaries using three different approaches. Problem Example Let's understand the problem with an example ? # Input list of dictionaries input_list = [ {'hello': 3, 'tutorialspoint': 6}, {'python': 9}, ... Read More
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of key-value pairs, where each key corresponds to its associated value. URL parameters (query strings) often need to be parsed into dictionaries for easier manipulation in web applications. This article demonstrates three different methods to convert URL parameters into Python dictionary items. Methods Overview The following approaches will be covered ? Using urllib.parse.parse_qs() function Using setdefault() and re.findall() functions Using split() function Example Input and Output For all methods, we'll use ... Read More
In Python, an array (list) is a collection of comma-separated values enclosed in square brackets. Lists are flexible as their elements don't need to be of the same type. In this article, we will learn how to check whether a number formed by combining all elements of an array is a palindrome ? Methods Used The following are the various methods to accomplish this task ? Using map() & join() functions Using type casting & string concatenation Using str(), list(), extend(), join() and reverse() functions Example Scenario Consider an input list. We ... Read More
In Python, a dictionary is an implementation of a data structure known as an associative array. A dictionary is made up of key-value pairs, where each key maps to its corresponding value. In this article, we will learn how to extract unique values from a dictionary and convert them into a set, which automatically removes duplicates. Example Overview Let's start with an example to understand what we want to achieve ? # Input dictionary with duplicate values input_dict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5} print("Input dictionary:", input_dict) print("Dictionary values:", list(input_dict.values())) # ... Read More
Python dictionaries are ordered collections of key-value pairs. Sometimes you need to rotate a dictionary by K positions, which means shifting all key-value pairs by K positions in their order. This article demonstrates how to rotate a dictionary by K positions using two different approaches. Understanding Dictionary Rotation Dictionary rotation shifts all key-value pairs by K positions. For example, rotating {2: 5, 4: 6, 1: 3} by 1 position gives {1: 3, 2: 5, 4: 6}. Example Input and Output # Input dictionary inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} ... Read More
In this article, we will learn how to retain the first N characters of a string and replace the remaining characters with a specified character K. Python provides several built-in functions like len(), slicing, replace(), and ljust() to accomplish this task efficiently. Problem Overview Given an input string, we need to keep the first N characters unchanged and replace all remaining characters with a replacement character K. Example Let's understand this with a simple example ? # Input input_string = 'tutorialspoint' n = 9 k = "#" # Expected output: 'tutorials#####' print(f"Original: {input_string}") ... Read More
Python provides several methods to replace all occurrences of a character except the first occurrence. This is useful when you want to preserve the first instance while modifying subsequent occurrences. In Python, strings are immutable sequences of characters. When we need to replace specific occurrences, we can use built-in functions like slicing() and replace() or implement custom logic using loops. Problem Statement Given an input string and a replacement character k, replace all occurrences of the first character except the very first occurrence. Example For the string 'blueforblueforblue' with replacement character '*' ? ... Read More
In Python, we can find numbers that exist within a specific range but are not present in a given set using several approaches: the not operator, set subtraction, or the Counter function. A Python set is a collection of unordered, unique elements. Sets are mutable, meaning you can add or remove elements after creation, but the elements themselves must be immutable and distinct. Problem Example Given an input set and a range, find all numbers in the range that don't exist in the set ? Input input_set = {3, 10, 2, 11, 15, 4, ... Read More
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value. In this article, we will learn how to extract keys with specific value type in Python using three different approaches. Methods Used The following are the various methods to accomplish this task − Using for loop and isinstance() method Using list comprehension and isinstance() method Using keys() & type() methods Problem Example Let's consider ... Read More
A character frequency string contains characters followed by their frequency count. For example, 'p5y3t6' means 'p' appears 5 times, 'y' appears 3 times, and 't' appears 6 times. Expanding this string produces 'pppppyyytttttt'. In this article, we will learn how to expand character frequency strings using different Python approaches. Problem Statement Given an input string where characters alternate with their frequencies, expand each character according to its frequency count ? Example inputString = 'p5y3t6h2o1n4' print("Input:", inputString) print("Expected Output: pppppyyytttttthhonnnn") Input: p5y3t6h2o1n4 Expected Output: pppppyyytttttthhonnnn Method 1: Using zip() and ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance