Python Articles - Page 1001 of 1048

How to recursively iterate a nested Python dictionary?

Niharikaa Aitam
Updated on 07-Jun-2025 22:42:30

10K+ Views

A Dictionary in Python is a mutable, unordered collection of data in a key-value format. A dictionary can also contain another dictionary as a value and this is known as a Nested Dictionary or a dictionary within a dictionary. When working with this type of data structure, it’s often important to go through each key-value pair at every level. In this article, we will explore how to recursively iterate through a nested Python dictionary using a function-based approach. Syntax to iterate through a Dictionary recursively Following is a sample syntax for recursively iterating through a nested dictionary - def recursive_iter(d): ... Read More

How to match a non-whitespace character in Python using Regular Expression?

Niharikaa Aitam
Updated on 09-Jun-2025 09:55:51

4K+ Views

A non-whitespace character is any character that is not a space, tab i.e., \t, newline i.e., or carriage return \r. Examples of non-whitespace characters such as letters, digits, punctuation and special symbols. In Python, Regular Expressions (RegEx) are used to match the patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering and much more based on string patterns. To match a non-whitespace character in Python we can use the special character class \S inside a raw string in any ... Read More

How to match non-word characters in Python using Regular Expression?

Niharikaa Aitam
Updated on 08-Jun-2025 10:26:33

2K+ Views

A Non-word character is any type of character that is not a letter, digit, or underscore, i.e., anything other than [a-z, A-Z, 0-9_]. Examples of non-word characters are symbols, punctuation marks, spaces, tabs, and other special characters. In Python, Regular Expressions (RegEx) are used to match the patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering and much more, based on string patterns. To match a non-word character in Python, we can use the special character class \W inside ... Read More

How to match a word in python using Regular Expression?

Niharikaa Aitam
Updated on 08-Jun-2025 12:44:29

5K+ Views

In Python, Regular Expressions (RegEx) are used to match patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering, and much more based on string patterns. In this article, we will explore different ways to match a word in Python using the re module. Match a Specific Word using re.search() In Python, the re.search() method is used to search for a pattern within a given string. If the pattern is found, then it returns a match object otherwise, it returns ... Read More

How to define a Python dictionary within dictionary?

Niharikaa Aitam
Updated on 07-Jun-2025 22:46:35

286 Views

A Dictionary in Python is a mutable, unordered collection of data in a key-value format. A dictionary can also contain another dictionary as a value, and this is known as a Nested Dictionary or a dictionary within a dictionary. In this article, we are going to explore all the different ways to use a dictionary within a dictionary in Python. Syntax to define a Dictionary within another Following is the syntax of creating a Dictionary within another Dictionary in Python - outer_dict = { 'key1': { 'inner_key1': ... Read More

How you will create your first program in Python?

Niharikaa Aitam
Updated on 29-May-2025 05:01:40

193 Views

When we are starting with Python, mostly created first program will be "Hello, World", which prints the statement "Hello, World" as the output. Before we begin with the first program, we need to install Python in our system from the official website. Choose a Code Editor After installing Python, we need to select the text editor based on our requirements and usage. Following are the different editors available - IDLE: It comes pre-installed with Python VS Code: A popular and lightweight code editor PyCharm: This is useful ... Read More

How to remove a key from a python dictionary?

Niharikaa Aitam
Updated on 28-May-2025 14:52:59

933 Views

A dictionary in Python is a collection of key-value pairs. In a few cases, we may have to remove a key and its associated value from a dictionary. Python provides multiple ways to do this safely and efficiently. Using pop() method The pop() method is used to remove the specified key and returns the corresponding value. If the key does not exist, then it raises a KeyError unless a default value is provided. Example Following is an example, which shows how to remove a key from the dictionary using the pop() method - my_dict = {'a': 1, 'b': 2, 'c': ... Read More

How to add new keys to a dictionary in Python?

SaiKrishna Tavva
Updated on 01-Sep-2025 14:57:36

1K+ Views

In Python, a Dictionary is one of the data structures that contains key-value pairs enclosed in curly braces '{}'. It is a Mutable and unordered data structure. Before proceeding with adding new keys to a dictionary, first let's create a dictionary with key and values. Following is the example of creating a dictionary with specified key and values - dict1 = {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'} print("Created Dictionary:", dict1) Following is the output of the above example - Created Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'} Adding new keys to a Dictionary Once a Dictionary is ... Read More

How to merge two Python dictionaries in a single expression?

SaiKrishna Tavva
Updated on 01-Sep-2025 14:52:24

263 Views

In Python, a Dictionary is a unordered and muttable data structure which stores data as a key-value pair. In some cases, we may need to merge two dictionaries into a single dictionary by using a single expression, in such scenarios python provides different methods. Using | Merge Operator The | merge operator in Python is used to merge multiple dictionaries into one with a single expression and returns the combined dictionary as a new dictionary. If both dictionaries have the same key, then the value from the dictionary on the right side of the operator will be replaced with the ... Read More

How to change any data type into a string in Python?

Niharikaa Aitam
Updated on 29-May-2025 04:52:26

269 Views

In Python, converting different data types into strings is a common requirement when we want to display or store information in text format. The built-in str() function is used for this conversion. This function accepts any Python object as a parameter and returns a string representation of it. The basic syntax of the str() function is as follows: str(object) Where object is any valid Python object, such as int, float, bool, list, etc. In this article, we will explore different data type conversions into a string in Python. Conversion of Integer to String An integer can be converted into ... Read More

Advertisements