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 on Trending Technologies
Technical articles with clear explanations and examples
How do we create Python String from list?
In programming, it is very common to work with lists, which are collections of items. Sometimes, you may want to turn a list into a single string. In this chapter, we will show you how to create a Python string from a list using simple explanations and examples. Understanding Lists A list in Python is a way to store multiple values. For example, you can have a list of words like ? my_list = ["Hello", "world", "Python", "is", "great"] print(my_list) ['Hello', 'world', 'Python', 'is', 'great'] In this example, we have a ...
Read MoreHow to insert a Python tuple in a MySQL database?
Inserting a Python tuple into a MySQL database is a common task when working with structured data. This tutorial shows how to insert tuple data into a MySQL table using the PyMySQL module. Prerequisites Before starting, ensure you have: A MySQL database named test A table named employee with fields: fname, lname, age, gender, salary PyMySQL module installed (pip install PyMySQL) Sample Tuple Data Let's define a tuple containing employee data ? # Sample employee data as tuple employee_data = ('Mac', 'Mohan', 20, 'M', 2000) print("Employee data:", employee_data) ...
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 More