Programming Articles

Page 902 of 2547

Python Categorize the given list by string size

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

Let's consider a list containing many strings of different lengths. In this article we will see how to group those elements into categories where the strings are of equal length in each group. Using For Loop We design a for loop which will iterate through every element of the list and append it only to the group where its length matches with the length of existing elements ? Example days = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Given list print("Given list:") print(days) # Categorize by string size len_comp = lambda x, y: len(x) ...

Read More

Converting an image to ASCII image in Python

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

Converting an image to ASCII art involves transforming pixel brightness values into corresponding ASCII characters. This creates a text-based representation of the original image using characters like #, @, *, and . to represent different shades. Required Library We'll use the Pillow library for image processing − pip install Pillow ASCII Character Set First, define the ASCII characters arranged from darkest to lightest − ASCII_CHARS = ['#', '?', '%', '.', 'S', '+', '.', '*', ':', ', ', '@'] print("ASCII characters from dark to light:", ASCII_CHARS) ASCII characters from ...

Read More

Boolean list initialization in Python

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

There are scenarios when we need to create a list containing only Boolean values like True and False. Python provides several methods to initialize Boolean lists efficiently. Using List Comprehension with range() We can use list comprehension with range() to create a list of Boolean values. This approach gives us flexibility to set different values based on conditions ? # Create a list of True values bool_list = [True for i in range(6)] print("The list with Boolean elements is:", bool_list) # Create alternating True/False pattern alternating = [i % 2 == 0 for i in ...

Read More

Binary element list grouping in Python

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

When working with lists of pairs, you often need to group elements by a common value. Python provides several approaches to group sublists based on shared elements, typically grouping by the second element of each pair. Using set and map This approach extracts unique second elements using set() and map(), then groups first elements that share the same second element ? days_data = [['Mon', 2], ['Tue', 3], ['Wed', 3], ["Thu", 1], ['Fri', 2], ['Sat', 3], ...

Read More

Add the occurrence of each number as sublists in Python

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

When working with lists containing duplicate elements, you may need to create sublists that show each unique element paired with its frequency count. Python provides several approaches to accomplish this task effectively. Using For Loop and Append This approach compares each element with every other element in the list to count occurrences. We track processed elements to avoid duplicates in the result ? Example def count_occurrences_manual(numbers): result = [] processed = [] for i in range(len(numbers)): ...

Read More

Add only numeric values present in a list in Python

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

We have a Python list which contains both strings and numbers. In this article we will see how to sum up the numbers present in such list by ignoring the strings. Using filter() and isinstance() The isinstance() function can be used to filter out only the numbers from the elements in the list. Then we apply the sum() function to get the final result. Example mixed_data = [1, 14, 'Mon', 'Tue', 23, 'Wed', 14, -4] # Given list print("Given list:", mixed_data) # Add the numeric values using filter result = sum(filter(lambda i: isinstance(i, ...

Read More

Accessing index and value in a Python list

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

When working with Python lists, you often need to access both the index position and the value of elements. Python provides several methods to accomplish this, each suited for different use cases. Using list.index() to Find Element Position The index() method returns the position of a specific element in the list ? numbers = [11, 45, 27, 8, 43] print("Index of 45:", numbers.index(45)) days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] print("Index of Wed:", days.index('Wed')) Index of 45: 1 Index of Wed: 3 Using range() and len() for All ...

Read More

Get match indices in Python

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

When working with two lists in Python, you often need to find the indices of elements in the first list that match values in the second list. Python provides several approaches to accomplish this task efficiently. Using index() Method The simplest approach uses list comprehension with the index() method to find the position of each matching element ? days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] target_days = ['Tue', 'Fri'] # Given lists print("The given list:", days) print("The list of values:", target_days) # Using index() method match_indices = [days.index(item) for item in target_days] # ...

Read More

Get last N elements from given list in Python

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

Getting the last N elements from a Python list is a common operation. Python provides several approaches, with slicing being the most straightforward and itertools.islice() offering memory-efficient alternatives for large datasets. Using List Slicing The simplest method uses negative indexing with slicing. The syntax list[-n:] extracts the last N elements ? Example days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Given list print("Given list:", days) # Get last 4 elements n = 4 result = days[-n:] print(f"The last {n} elements:", result) Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', ...

Read More

Get key with maximum value in Dictionary in Python

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

A Python dictionary contains key-value pairs. In this article we will see how to get the key of the element whose value is maximum in the given Python dictionary. Using max() with get() The max() function with the get() method is the most straightforward approach to find the key with maximum value ? Example scores = {"Mon": 3, "Tue": 11, "Wed": 8} print("Given Dictionary:") print(scores) # Using max and get max_key = max(scores, key=scores.get) print("The Key with max value:") print(max_key) The output of the above code is ? Given Dictionary: ...

Read More
Showing 9011–9020 of 25,466 articles
« Prev 1 900 901 902 903 904 2547 Next »
Advertisements