Server Side Programming Articles

Page 547 of 2109

Python - Ways to flatten a 2D list

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 1K+ Views

Flattening a 2D list means converting a nested list structure into a single-dimensional list. Python provides several methods to accomplish this task, each with different performance characteristics and use cases. Using itertools.chain.from_iterable() The chain.from_iterable() function efficiently flattens a 2D list by chaining all sublists together − from itertools import chain nested_list = [[1, 2, 3], [3, 6, 7], [7, 5, 4]] print("Initial list:", nested_list) ...

Read More

Python - Ways to find indices of value in list

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 280 Views

Finding indices of specific values in a list is a common task in Python programming. While the index() method finds the first occurrence, there are several ways to find all indices where a particular value appears in a list. Using enumerate() The enumerate() function returns both index and value, making it perfect for this task ? # initializing list numbers = [1, 3, 4, 3, 6, 7] print("Original list :", numbers) # using enumerate() to find indices for 3 indices = [i for i, value in enumerate(numbers) if value == 3] print("Indices of 3 :", ...

Read More

Python - Ways to create triplets from given list

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 449 Views

A triplet is a group of three consecutive elements from a list. Creating triplets from a given list means extracting all possible combinations of three adjacent elements. Python provides several approaches to accomplish this task efficiently. Using List Comprehension List comprehension provides a concise way to create triplets by slicing the list ? # List of words words = ['I', 'am', 'Vishesh', 'and', 'I', 'like', 'Python', 'programming'] # Using list comprehension to create triplets triplets = [words[i:i + 3] for i in range(len(words) - 2)] print("Triplets using list comprehension:") print(triplets) Triplets ...

Read More

Python - Ways to convert array of strings to array of floats

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 810 Views

When working with numerical data, you often need to convert arrays of string numbers to floating-point arrays for mathematical operations. NumPy provides several efficient methods to perform this conversion. Using astype() Method The astype() method is the most common way to convert data types in NumPy arrays − import numpy as np # Create array of string numbers string_array = np.array(["1.1", "1.5", "2.7", "8.9"]) print("Initial array:", string_array) # Convert to array of floats using astype float_array = string_array.astype(np.float64) print("Final array:", float_array) print("Data type:", float_array.dtype) Initial array: ['1.1' '1.5' '2.7' '8.9'] Final ...

Read More

Python - Using variable outside and inside the class and method

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 3K+ Views

Python is an object-oriented programming language where variables can be defined at different scopes. Understanding variable scope is crucial for writing clean, maintainable code. Variables can be defined outside classes (global scope), inside classes (class scope), or inside methods (local scope). Variables Defined Outside the Class (Global Variables) Variables defined outside any class or function have global scope and can be accessed from anywhere in the program ? # Variable defined outside the class (global scope) outVar = 'outside_class' print("Global access:", outVar) # Class one class Ctest: print("Inside class:", outVar) ...

Read More

Python - Prefix sum list

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 994 Views

A prefix sum list (also called cumulative sum) is a new list where each element represents the sum of all elements from the start up to that position in the original list. This is useful for range sum queries and sliding window problems. Using List Comprehension with sum() The most straightforward approach uses list comprehension with the sum() function and list slicing ? # using list comprehension + sum() + list slicing # initializing list test_list = [3, 4, 1, 7, 9, 1] # printing original list print("The original list : " + str(test_list)) ...

Read More

Python - Plotting Radar charts in excel sheet using XlsxWriter module

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 312 Views

A radar chart is a graphical method of displaying multivariate data in the form of a two-dimensional chart where three or more quantitative variables are represented on axes starting from the same point. Python's XlsxWriter module allows you to create radar charts directly in Excel files. Installing XlsxWriter First, install the XlsxWriter module if you haven't already ? pip install XlsxWriter Creating a Radar Chart Here's how to create a radar chart with sample data comparing two batches ? import xlsxwriter # Create a workbook and add a worksheet workbook ...

Read More

Python - Plotting an Excel chart with pattern fills in column using XlsxWriter module

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 262 Views

The XlsxWriter module allows you to create Excel charts with pattern fills in columns, making data visualization more distinctive and professional. Pattern fills help differentiate data series visually, especially when working with monochrome displays or printing. Understanding Pattern Fills Pattern fills apply textures or patterns to chart columns instead of solid colors. Common patterns include 'shingle', 'horizontal_brick', 'vertical_brick', and 'dots'. Each pattern can have foreground and background colors for customization. Example Let's create a column chart comparing different building materials with pattern fills − import xlsxwriter # Create workbook and worksheet workbook = ...

Read More

Python - Plotting an Excel chart with Gradient fills using XlsxWriter module

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 300 Views

XlsxWriter is a Python library for creating Excel files with advanced formatting, including charts with gradient fills. This tutorial shows how to create a column chart with gradient-filled data series. Installing XlsxWriter First, install the required module ? pip install xlsxwriter Creating Excel Chart with Gradient Fills The following example creates a column chart with two data series, each having different gradient colors ? import xlsxwriter # Create a workbook and add a worksheet workbook = xlsxwriter.Workbook('chart_gradient1.xlsx') worksheet = workbook.add_worksheet() # Create a bold format for headers bold = ...

Read More

Python - Number of values greater than K in list

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 907 Views

Counting the number of values greater than a specific threshold K in a list is a common programming task. Python provides several efficient approaches to solve this problem using different techniques. Using a For Loop The traditional approach uses a counter variable and iterates through each element − # Find number of elements > k using for loop numbers = [1, 7, 5, 6, 3, 8] k = 4 print("The list:", numbers) count = 0 for num in numbers: if num > k: ...

Read More
Showing 5461–5470 of 21,090 articles
« Prev 1 545 546 547 548 549 2109 Next »
Advertisements