Python Articles

Page 102 of 855

Python Get a list as input from user

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

In this article, we will see how to ask the user to enter elements of a list and create the list with those entered values. Python provides several approaches to collect user input and build lists dynamically. Using Loop with input() and append() The most straightforward approach is to ask for the number of elements first, then collect each element individually using a loop ? numbers = [] # Input number of elements n = int(input("Enter number of elements in the list : ")) # iterating till the range for i in range(0, n): ...

Read More

Python Generate successive element difference list

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

Finding the difference between successive elements in a list is a common operation in Python. This article explores three efficient methods to calculate consecutive element differences using list comprehension, list slicing, and the operator module. Using List Comprehension with Index The most straightforward approach uses list comprehension with index positions to access consecutive elements ? numbers = [12, 14, 78, 24, 24] # Given list print("Given list:", numbers) # Using index positions with list comprehension differences = [numbers[i + 1] - numbers[i] for i in range(len(numbers) - 1)] # Print result print("Successive differences:", ...

Read More

Python Generate random string of given length

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

In this article we will see how to generate a random string with a given length. This will be useful in creating random passwords or other programs where randomness is required. Using random.choices() The choices() function in the random module can produce multiple random characters which can then be joined to create a string of given length ? Example import string import random # Length of string needed N = 5 # With random.choices() res = ''.join(random.choices(string.ascii_letters + string.digits, k=N)) # Result print("Random string :", res) The output of the ...

Read More

Python Generate random numbers within a given range and store in a list

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

In this article we will see how to generate random numbers between a pair of numbers and store those values in a list. Python's random module provides the randint() function for this purpose. Syntax random.randint(start, end) Parameters: start − Lower bound (inclusive) end − Upper bound (inclusive) Both start and end should be integers Start should be less than or equal to end Using a Function with Loop In this example we use the range() function in a for loop. With help of append() we generate and add these random numbers ...

Read More

Python Categorize the given list by string size

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 268 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 679 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 297 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 225 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
Showing 1011–1020 of 8,549 articles
« Prev 1 100 101 102 103 104 855 Next »
Advertisements