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 Niharikaa Aitam
Page 2 of 8
How to create an empty dictionary in Python?
A dictionary is a fundamental data structure in Python that stores data as key-value pairs. Dictionaries are also known as associative arrays and are represented by curly braces {}. Unlike lists that use numeric indexes, dictionaries use unique keys to access their values. Keys must be immutable objects like strings, numbers, or tuples, while values can be of any data type. Python provides two simple methods to create an empty dictionary. Using Curly Braces {} The most common way to create an empty dictionary is using empty curly braces. Syntax variable_name = {} ...
Read MoreHow we can use Python Tuple within a Tuple?
In Python, a tuple is an ordered and immutable collection of elements created using parentheses. A powerful feature of tuples is their ability to contain other tuples as elements, creating nested tuples or tuples within tuples. Syntax The basic syntax for creating nested tuples is ? outer_tuple = (value1, value2, (nested_value1, nested_value2), value3) Note: Nested tuple elements are accessed using multiple index levels with square bracket notation. Creating a Basic Tuple Let's start with a simple tuple containing integer elements ? # Creating a basic tuple numbers = (20, 40, ...
Read MoreWhat is correct syntax to create Python dictionary?
A Dictionary is a collection of key-value pairs, where each key is separated from its value by a colon ":" and items are separated by commas. Dictionary keys must be unique and since Python 3.7, dictionaries maintain insertion order. The correct syntax to create a Python dictionary is to store values in key-value pairs within curly braces { }. Keys appear on the left of the colon, values on the right, and items are separated by commas. Basic Syntax The fundamental syntax for creating a dictionary in Python ? my_dict = { ...
Read MoreHow to count elements in a nested Python dictionary?
A nested dictionary is a dictionary inside another dictionary. In Python, we have the function len() to count elements in a nested dictionary. We can also use recursive functions to calculate elements at arbitrary depths within nested structures. Non-Recursive Count in Nested Dictionary A non-recursive count means counting only the keys at the top level, without going into any nested dictionaries. This approach uses the built-in len() function ? # Define a nested dictionary nested_dict = { "name": "Alice", "age": 30, "address": { ...
Read MoreHow to truncate a Python dictionary at a given length?
Truncating a dictionary in Python means limiting it to a fixed number of key-value pairs. From version Python 3.7 onwards, dictionaries maintain 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 explore different methods to truncate a Python dictionary at a given length. Using itertools.islice() Method The itertools.islice() method truncates a dictionary by slicing its items without loading all items into memory. This method is ...
Read MoreHow to print Python dictionary into JSON format?
In Python, dictionaries are used to store structured data in key-value pairs. When we need to display this data in a readable format or send it over the internet (such as to an API), we convert the dictionary to JSON (JavaScript Object Notation) format. Python provides a built-in json module to handle dictionary-to-JSON conversions seamlessly. JSON is a lightweight, text-based data interchange format that's easy for humans to read and write. Originally derived from JavaScript, it's now language-independent and widely used for data exchange. Creating a Sample Dictionary Let's start by creating a dictionary with product ...
Read MoreHow to merge multiple Python dictionaries?
In Python, a dictionary is a collection of key-value pairs used for data retrieval. In some cases, we may need to combine two or more dictionaries into one. This is known as merging dictionaries. Python provides several methods to merge multiple dictionaries, each with different characteristics and use cases. Using "|" Merge Operator The "|" merge operator (available in Python 3.9+) merges multiple dictionaries and returns a new dictionary. If both dictionaries have the same key, the value from the right dictionary takes precedence. Example: Merging Two Dictionaries The following example merges two dictionaries using ...
Read MoreHow to recursively iterate a nested Python dictionary?
A Dictionary in Python is a mutable collection of data in a key-value format. A dictionary can also contain another dictionary as a value, creating a Nested Dictionary or a dictionary within a dictionary. When working with nested data structures, you often need to access each key-value pair at every level using recursive iteration. In this article, we will explore how to recursively iterate through a nested Python dictionary using different approaches. Basic Syntax Here's the basic syntax for recursively iterating through a nested dictionary ? def recursive_iter(d): for key, value ...
Read MoreHow to match a non-whitespace character in Python using Regular Expression?
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 include letters, digits, punctuation and special symbols. In Python, Regular Expressions (RegEx) are used to match patterns in input strings. The re module in Python provides different methods to use regular expressions. This module helps developers 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 with any re module method. In ...
Read MoreHow to match non-word characters in Python using Regular Expression?
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 ...
Read More