Programming Articles

Page 334 of 2547

Python program to convert a list into a list of lists using a step value

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 263 Views

When working with lists in Python, you may need to convert a flat list into a list of lists using a specific step value. This allows you to group elements and create nested structures from your data. Method 1: Using List Slicing with Step Value The most efficient approach uses list slicing to chunk the original list into smaller sublists ? def convert_list_with_step(data, step): result = [] for i in range(0, len(data), step): result.append(data[i:i+step]) return result ...

Read More

Python Program to find the cube of each list element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

Finding the cube of each list element is a common operation in Python. We can achieve this using several approaches: simple iteration with append(), list comprehension, or the map() function. Method 1: Using Simple Iteration This approach uses a for loop to iterate through each element and append the cubed value to a new list − numbers = [45, 31, 22, 48, 59, 99, 0] print("The list is:") print(numbers) cubed_numbers = [] for num in numbers: cubed_numbers.append(num * num * num) print("The resultant list is:") print(cubed_numbers) ...

Read More

Print all words occurring in a sentence exactly K times

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 465 Views

When you need to print all words occurring in a sentence exactly K times, you can use Python's string methods like split(), count(), and remove(). This approach counts word frequency and displays words that match the specified frequency. Example Below is a demonstration of finding words with exact frequency ? def key_freq_words(my_string, K): my_list = list(my_string.split(" ")) for i in my_list: if my_list.count(i) == K: print(i) ...

Read More

Python - Custom space size padding in Strings List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 436 Views

When working with string lists in Python, you may need to add custom spacing around each string element. This can be achieved using string concatenation with multiplied spaces or built-in string methods like center(). Method 1: Using String Concatenation You can add custom leading and trailing spaces by multiplying space characters and concatenating them ? my_list = ["Python", "is", "great"] print("The list is:") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The ...

Read More

Python - First occurrence of one list in another

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 428 Views

When it is required to find the first occurrence of one list in another list, Python provides several approaches. The most efficient method uses the set data structure for fast lookups combined with the next() function for early termination. Using next() with Set Conversion This approach converts the second list to a set for O(1) lookup time and uses next() to return the first match ? my_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) ...

Read More

Python - Merge Pandas DataFrame with Inner Join

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To merge Pandas DataFrame, use the merge() function. An inner join returns only the rows that have matching values in both DataFrames. It's implemented by setting the how parameter to "inner". Syntax pd.merge(left, right, on='column_name', how='inner') How Inner Join Works An inner join combines rows from two DataFrames where the join condition is met. Only matching records from both DataFrames are included in the result. Creating Sample DataFrames Let's create two DataFrames with some common and different car models ? import pandas as pd # Create DataFrame1 dataFrame1 = ...

Read More

Python - Calculate the variance of a column in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To calculate the variance of column values in a Pandas DataFrame, use the var() method. Variance measures how spread out the data points are from the mean value. Syntax The basic syntax for calculating variance is ? DataFrame['column_name'].var() Creating a DataFrame First, import the required Pandas library and create a DataFrame ? import pandas as pd # Create DataFrame with car data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") ...

Read More

Python - How to reset index after Groupby pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 9K+ Views

When you perform a groupby operation in pandas, the grouped column becomes the index. To convert this index back to a regular column, use reset_index(). Import Required Library First, import pandas ? import pandas as pd Creating Sample DataFrame Let's create a DataFrame with car names and prices ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] }) print("Original DataFrame:") print(dataFrame) ...

Read More

Python - Sum only specific rows of a Pandas Dataframe

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 5K+ Views

To sum only specific rows in a Pandas DataFrame, use the loc[] method to select specific row ranges and columns. The loc[] method allows you to specify both row indices and column names for precise data selection. Let's start by creating a DataFrame with product inventory data ? import pandas as pd # Create DataFrame with product inventory data dataFrame = pd.DataFrame({ "Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("Original DataFrame:") print(dataFrame) ...

Read More

Python Pandas – Find the common rows between two Data Frames

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 5K+ Views

To find the common rows between two DataFrames in Pandas, use the merge() method with how='inner'. This returns only the rows that have identical values across all columns in both DataFrames. Creating Sample DataFrames Let us first create two DataFrames with car data ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) DataFrame1: Car Units 0 ...

Read More
Showing 3331–3340 of 25,466 articles
« Prev 1 332 333 334 335 336 2547 Next »
Advertisements