Programming Articles

Page 503 of 2547

Last occurrence of some element in a list in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 11K+ Views

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 More

Linear search on list or tuples in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

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 More

List consisting of all the alternate elements in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 3K+ Views

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 More

List expansion by K in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 265 Views

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 More

Python - List Initialization with alternate 0s and 1s

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 1K+ Views

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 More

Python - Make pair from two list such that elements are not same in pairs

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 5K+ Views

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 More

Prefix matching in Python using pytrie module

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 520 Views

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

Fetching text from Wikipedia's Infobox in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 1K+ Views

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 More

How to select multiple DataFrame columns using regexp and datatypes

Kiran P
Kiran P
Updated on 25-Mar-2026 876 Views

A DataFrame is a two-dimensional tabular data structure with rows and columns, similar to a spreadsheet or database table. Selecting specific columns efficiently is crucial for data analysis. This article demonstrates how to select DataFrame columns using regular expressions and data types. Creating a Sample DataFrame Let's start by creating a DataFrame from a movies dataset ? import pandas as pd # Create DataFrame from CSV movies_dataset = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv") # Display basic info print(type(movies_dataset)) print(movies_dataset.head()) budget id original_language original_title popularity release_date ...

Read More

Program to find airports in correct order in Python?

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 434 Views

Finding airports in correct order from a shuffled list of flights is a classic graph traversal problem. We need to reconstruct the travel itinerary by finding an Eulerian path through the flight connections, ensuring lexicographical ordering when multiple valid paths exist. Algorithm Overview The solution uses Hierholzer's algorithm to find an Eulerian path in a directed graph ? Build adjacency list from flight pairs Track in-degree and out-degree for each airport Find starting airport (out-degree - in-degree = 1, or lexicographically smallest) Use DFS to traverse and build the path Return reversed path for correct order ...

Read More
Showing 5021–5030 of 25,466 articles
« Prev 1 501 502 503 504 505 2547 Next »
Advertisements