Server Side Programming Articles

Page 900 of 2109

Count distinct elements in an array in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 692 Views

In Python lists, we often encounter duplicate elements. While len() gives us the total count including duplicates, we sometimes need to count only the distinct (unique) elements. Python provides several approaches to accomplish this task. Using Counter from collections The Counter class from the collections module creates a dictionary where elements are keys and their frequencies are values. We can use its keys() method to get distinct elements ? from collections import Counter days = ['Mon', 'Tue', 'Wed', 'Mon', 'Tue'] print("Length of original list:", len(days)) distinct_elements = Counter(days).keys() print("List with distinct elements:", list(distinct_elements)) print("Length ...

Read More

How to style a select dropdown with only CSS?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 282 Views

The element creates dropdown lists for forms, but its default appearance is limited. While complete customization of native select elements has restrictions, we can apply various CSS properties to improve their styling and create visually appealing dropdowns. Syntax select { property: value; } select option { property: value; } Key Properties for Select Styling PropertyDescription appearanceRemoves default browser styling borderControls border style and thickness border-radiusAdds rounded corners paddingControls internal spacing background-colorSets background color font-sizeControls text size Example: Basic Select Styling ...

Read More

Backward iteration in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 7K+ Views

Sometimes we need to iterate through a list in reverse order − reading the last element first, then the second-to-last, and so on. Python provides three common approaches: range() with negative step, slice notation [::-1], and the reversed() built-in function. Using range() with Negative Step Start from the last index and step backwards by -1 until index 0 ? days = ['Mon', 'Tue', 'Wed', 'Thu'] for i in range(len(days) - 1, -1, -1): print(days[i]) Thu Wed Tue Mon range(3, -1, -1) generates indices 3, 2, 1, ...

Read More

append() and extend() in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

The append() and extend() methods both add elements to a Python list, but they behave differently. append() adds its argument as a single element (even if it's a list), while extend() adds each element of an iterable individually. append() Adds the argument as one element to the end of the list. List length increases by exactly 1, regardless of the argument type. days = ['Mon', 'Tue', 'Wed'] print("Original:", days) # Append a single element days.append('Thu') print("After append('Thu'):", days) # Append a list — added as a nested list days.append(['Fri', 'Sat']) print("After append(['Fri', 'Sat']):", days) ...

Read More

Add a row at top in pandas DataFrame

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

Adding a row at the top of a Pandas DataFrame is a common operation when you need to insert headers, summary rows, or new data at the beginning. There are several methods to achieve this − using pd.concat(), loc[] with index manipulation, or iloc slicing. Create a Sample DataFrame import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['Delhi', 'Mumbai', 'Pune'] }) print("Original DataFrame:") print(df) Original DataFrame: Name ...

Read More

Absolute and Relative frequency in Pandas

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 937 Views

In statistics, frequency indicates how many times a value appears in a dataset. Absolute frequency is the raw count, while relative frequency is the proportion (count divided by total observations). Pandas provides built-in methods for calculating both. Absolute Frequency Using value_counts() The simplest way to count occurrences of each value ? import pandas as pd data = ["Chandigarh", "Hyderabad", "Pune", "Pune", "Chandigarh", "Pune"] df = pd.Series(data).value_counts() print(df) Pune 3 Chandigarh 2 Hyderabad 1 dtype: int64 ...

Read More

Absolute Deviation and Absolute Mean Deviation using NumPy

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

In statistics, data variability measures how dispersed values are in a sample. Two key measures are Absolute Deviation (difference of each value from the mean) and Mean Absolute Deviation (MAD) (average of all absolute deviations). NumPy provides efficient functions to calculate both. Formulas $$\mathrm{Absolute\:Deviation_i = |x_i - \bar{x}|}$$ $$\mathrm{MAD = \frac{1}{n}\sum_{i=1}^{n}|x_i - \bar{x}|}$$ Where xi is each data point, x̄ is the mean, and n is the sample size. Absolute Deviation Calculate the absolute deviation for each element in a data sample ? from numpy import mean, absolute data = [12, ...

Read More

A += B Assignment Riddle in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 192 Views

The += operator on a mutable object inside a tuple creates a surprising Python riddle − the operation succeeds (the list gets modified) but also raises a TypeError because tuples don't support item assignment. Both things happen simultaneously. The Riddle Define a tuple with a list as one of its elements, then try to extend the list using += ? tupl = (5, 7, 9, [1, 4]) tupl[3] += [6, 8] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment ...

Read More

Add new column in Pandas Data Frame Using a Dictionary

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

A Pandas DataFrame is a two-dimensional tabular data structure with rows and columns. You can add a new column by mapping values from a Python dictionary to an existing column using the map() function. Creating a DataFrame First, create a DataFrame from a Pandas Series ? import pandas as pd s = pd.Series([6, 8, 3, 1, 12]) df = pd.DataFrame(s, columns=['Month_No']) print(df) Month_No 0 6 1 8 2 ...

Read More

Accessing elements of a Pandas Series

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 11K+ Views

A Pandas Series is a one-dimensional labeled array that can hold any data type. Elements can be accessed using integer position, custom index labels, or slicing. Creating a Series import pandas as pd s = pd.Series([11, 8, 6, 14, 25], index=['a', 'b', 'c', 'd', 'e']) print(s) a 11 b 8 c 6 d 14 e 25 dtype: int64 Accessing a Single Element Use integer position or custom label to access individual elements ? ...

Read More
Showing 8991–9000 of 21,090 articles
« Prev 1 898 899 900 901 902 2109 Next »
Advertisements