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
Server Side Programming Articles
Page 499 of 2109
Python - Joining only adjacent words in list
In this article, we are going to learn how to join adjacent words in a list while keeping digits separate. This technique is useful when processing mixed data that contains both text and numeric values. Approach To solve this problem, we need to ? Initialize the list with mixed string and numeric elements Separate words from digits using isalpha() and isdigit() methods Join all the words together using the join() method Add all digits to the result list while maintaining their order Using List Comprehension The most straightforward approach uses list comprehension to ...
Read MorePython - Joining unicode list elements
In Python, when working with Unicode strings, you might need to join list elements that contain Unicode characters. This is straightforward in Python 3 since all strings are Unicode by default. Basic Unicode String Joining The simplest way to join Unicode strings is using the join() method ? # Unicode strings with special characters unicode_strings = ['Hello', 'Wörld', 'Pythön', '🐍'] # Join with space separator result = ' '.join(unicode_strings) print(result) Hello Wörld Pythön 🐍 Joining Mixed Content Lists When your list contains mixed data types, convert them to strings ...
Read MorePython - Largest number possible from list of given numbers
In this article, we will learn how to find the largest possible number from a given list of numbers by arranging them optimally. We'll explore two different approaches to solve this problem effectively. Method 1: Using itertools.permutations The first approach generates all possible permutations of the numbers and finds the maximum value ? import itertools # initializing the list numbers = [45, 35, 138, 43, 67] # result list to store all permutations result = [] # generate all permutations and join them as strings for permutation in itertools.permutations(str(number) for number in numbers): ...
Read MoreLast occurrence of some element in a list in Python
Finding the last occurrence of an element in a list is a common task in Python. There are several efficient approaches to accomplish this, each with different advantages depending on your specific needs. Using Reverse and Index Method The first approach reverses the list and finds the first occurrence, then calculates the original position ? # initializing the list words = ['eat', 'sleep', 'drink', 'sleep', 'drink', 'sleep', 'go', 'come'] element = 'sleep' # reversing the list words.reverse() # finding the index of element index = words.index(element) # printing the final index final_index = ...
Read MoreLinear search on list or tuples in Python
Linear search is a simple algorithm that searches for an element by checking each item in a sequence from start to end. In Python, we can implement linear search for both lists and tuples using the same approach since both are iterable sequences. A linear search starts from the first element and continues until it finds the target element or reaches the end of the sequence. It's the most straightforward searching algorithm with O(n) time complexity. How Linear Search Works The algorithm follows these steps: Start from the first element of the list or tuple ...
Read MoreList 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 More