Found 33676 Articles for Programming

What is correct syntax to create Python lists?

Niharikaa Aitam
Updated on 28-May-2025 16:48:27

3K+ Views

In Python, a list is a built-in data structure that is used to store a sequence of items. Lists are mutable, i.e., the elements can be changed after the list is created. Syntax to create a Python List The correct syntax to create a Python List is using square brackets "[ ]". We need to specify the elements in the list by separating them with commas. Here is the basic syntax to create a list in Python - my_list = [item1, item2, item3, ...] Example: List of Integers A list of integers in Python is a sequence of whole numbers ... Read More

How to count elements in a nested Python dictionary?

Niharikaa Aitam
Updated on 10-Jun-2025 17:44:49

18K+ Views

A Nested dictionary is a dictionary inside another dictionary. In Python, we have the function len() to count elements in a Nested Dictionary. With that, we can also use a function that recursively calls and calculates the elements in an arbitrary depth of the nested dictionary. Non-Recursive Count in Nested Dictionary A non-recursive count in a nested dictionary means counting only the keys at the top level, without going into any nested dictionaries inside it. Here is an example of counting the elements in a nested python dictionary- # Define a nested dictionary nested_dict = { ... Read More

How to truncate a Python dictionary at a given length?

Niharikaa Aitam
Updated on 10-Jun-2025 17:47:50

3K+ Views

Truncating a dictionary in Python means limiting it to a fixed number of key-value pairs. From version Python 3.7 onwards, the dictionaries store the values based on the insertion order, which makes it easy to slice and rebuild a smaller dictionary using the first N items. This is helpful when we want to reduce data size or process only a portion of the dictionary. In this article, we are going to explore the different methods to truncate a Python dictionary at a given length. Using itertools.islice() Method The itertools.islice() method in Python is used to truncate a dictionary by slicing ... Read More

How to insert new keys/values into Python dictionary?

Niharikaa Aitam
Updated on 11-Jun-2025 15:26:06

65K+ Views

A dictionary in Python is a mutable, unordered collection of key-value pairs.Inserting keys/values into a Python dictionaryWe can add new key-value pairs to an existing dictionary simply by assigning a value to a new key. In this article, we are going to explore the different ways to insert new key values into a Python dictionary. Following is an example, which creates a dictionary with 4 key-value pairs and displays the dictionary - # Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print(myprod) # Displaying ... Read More

How to update a Python dictionary values?

Niharikaa Aitam
Updated on 09-Jun-2025 09:34:02

73K+ 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 ordered data structure. Before proceeding with updating the values of a dictionary, let's create an example Dictionary with 4 key-value pairs, in which Product, Model, Units, are keys of the Dictionary - # Creating a Dictionary with 4 key-value pairs myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print(myprod) # Displaying individual values ... Read More

How to print Python dictionary into JSON format?

Niharikaa Aitam
Updated on 20-Jun-2025 19:07:27

8K+ Views

In Python, Dictionaries are used to store structured data. When we want to print this data in a human-readable format or transfer it through the internet, such as to an API, then we need to convert the dictionary to JSON, i.e., JavaScript Object Notation. Python has a built-in module named json to handle such conversions. JSON is JavaScript Object Notation which is a lightweight text-based open standard designed for human-readable data interchange. The JSON format was specified by Douglas Crockford. It has been extended from the JavaScript scripting language. A dictionary in Python is a mutable and unordered collection ... Read More

How to merge multiple Python dictionaries?

Niharikaa Aitam
Updated on 20-Jun-2025 18:57:20

347 Views

In Python, a dictionary is a collection of key-value pairs that is used for data retrieval. In some cases, we may need to combine two or more dictionaries into one. This is known as merging dictionaries. In this article, we will explore different methods to merge multiple dictionaries in Python. Using "|" Merge Operator The "|" merge operator in Python is used to merge multiple dictionaries into one 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 ... Read More

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

Advertisements