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 Hafeezul Kareem
Page 2 of 26
List consisting of all the alternate elements in Python
In Python, getting alternate elements from a list means extracting every second element, typically starting from either index 0 (even positions) or index 1 (odd positions). This article demonstrates two efficient methods to accomplish this task. Method 1: Using List Comprehension with Range This approach uses list comprehension combined with range and modulo operator to filter elements at odd indices − # Initializing the list numbers = [1, 2, 3, 4, 5, 6, 7, 8] # Finding alternate elements (odd indices) result = [numbers[i] for i in range(len(numbers)) if i % 2 != 0] ...
Read MoreList expansion by K in Python
In this article, we are going to learn how to expand a list by replicating each element K times. We'll explore two different approaches to solve this problem efficiently. Method 1: Using List Replication Operator This method uses the multiplication operator to replicate elements and concatenates them to build the expanded list ? # initializing the list numbers = [1, 2, 3] K = 5 # empty list result = [] # expanding the list for i in numbers: result += [i] * K # printing the list print(result) ...
Read MorePython - List Initialization with alternate 0s and 1s
In this article, we will learn how to initialize a list with alternate 0s and 1s. Given a list length, we need to create a list where elements alternate between 1 and 0 starting with 1. Method 1: Using a For Loop The simplest approach is to iterate through the range and append values based on whether the index is even or odd ? # initializing an empty list result = [] length = 7 # iterating through the range for i in range(length): # checking if index is even or ...
Read MorePython - Make pair from two list such that elements are not same in pairs
In this article, we are going to learn how to make pairs from two lists such that no similar elements make a pair. This is useful when you need to create combinations where each pair contains different values. Using List Comprehension The most straightforward approach is to use nested list comprehension with a condition to filter out pairs with identical elements − # initializing the lists numbers_1 = [1, 2, 3, 4, 5] numbers_2 = [5, 8, 7, 1, 3, 6] # making pairs result = [(i, j) for i in numbers_1 for j in ...
Read MorePrefix matching in Python using pytrie module
In this article, we will learn about the pytrie module for prefix matching strings from a list of strings. The pytrie module provides efficient trie data structures for fast prefix-based operations. What is Prefix Matching? Prefix matching finds all strings in a collection that start with a given prefix. For example ? Input: List: ['tutorialspoint', 'tutorials', 'tutorialspython', 'python'] Prefix: 'tutorials' Output: ['tutorialspoint', 'tutorials', 'tutorialspython'] Installing pytrie First, install the pytrie module using pip ? pip install pytrie Using pytrie.StringTrie The pytrie.StringTrie data structure allows us to create, ...
Read MoreFetching text from Wikipedia's Infobox in Python
In this article, we are going to scrape the text from Wikipedia's Infobox using BeautifulSoup and requests in Python. We can do it in 10 minutes. It's straightforward and useful for extracting structured information from Wikipedia pages. Prerequisites We need to install bs4 and requests. Execute the below commands to install ? pip install beautifulsoup4 pip install requests Steps to Extract Infobox Data Follow the below steps to write the code to fetch the text that we want from the infobox ? Import the bs4 and requests modules. Send an HTTP ...
Read Moretime.process_time() function in Python
The time.process_time() function in Python returns the sum of system and user CPU time consumed by the current process. Unlike time.time(), it excludes time spent sleeping and focuses only on actual processing time. Syntax time.process_time() This function returns a float value representing the CPU time in seconds. Basic Example Here's how to get the current process time ? import time # Get current process time current_time = time.process_time() print(f"Current process time: {current_time} seconds") Current process time: 0.015625 seconds Measuring Execution Time The most common ...
Read Moretime.perf_counter() function in Python
In this tutorial, we are going to learn about the time.perf_counter() method in Python. This function is part of the time module and provides the highest available resolution to measure a short duration. The method time.perf_counter() returns a float value representing time in seconds. It includes time elapsed during sleep and is system-wide, making it ideal for measuring elapsed time in code execution. Basic Usage Let's start with a simple example to see how perf_counter() works ? import time # Get current performance counter value print("Current time:", time.perf_counter()) Current time: 263.3530349 ...
Read MoreThe most occurring number in a string using Regex in python
In this tutorial, we will write a regex that finds the most occurring number in a string using Python. We'll use the re module for pattern matching and collections.Counter for frequency counting. Steps to Find the Most Occurring Number Import the re and collections modules Initialize the string containing numbers Find all numbers using regex and store them in a list Find the most occurring number using Counter from collections module Example Here's how to implement this solution ? # importing the modules import re import collections # initializing the string ...
Read MoreTaking multiple inputs from user in Python
In this tutorial, we are going to learn how to take multiple inputs from the user in Python. The data entered by the user will be in the string format. So, we can use the split() method to divide the user entered data. Taking Multiple String Inputs Let's take multiple strings from the user using split() ? # taking the input from the user strings = input("Enter multiple names space-separated:- ") # splitting the data strings = strings.split() # printing the data print(strings) The output of the above code is ? ...
Read More