Python Articles

Page 817 of 855

Element with largest frequency in list in Python

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

Finding the element with the largest frequency in a list is a common task in data analysis and statistics. Python provides several built-in approaches to accomplish this efficiently using the collections.Counter class and the statistics.mode function. Using Counter from collections The Counter class provides a most_common() method that returns elements with their frequencies in descending order. You can pass a parameter to limit the number of results ? Example from collections import Counter # Given list days_and_numbers = ['Mon', 'Tue', 'Mon', 9, 3, 3] print("Given list:", days_and_numbers) # Find the single most ...

Read More

Element repetition in list in Python

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

There are scenarios when we need to repeat the values in a list. This duplication of values can be achieved in Python using several approaches. Let's explore different methods to duplicate each element in a list. Using List Comprehension with Nested Loop This is a straightforward approach where we use list comprehension with nested iteration to duplicate each element ? # Given list data = ['Mon', 'Tue', 9, 3, 3] print("Given list:", data) # Adding another element for each element new_list = [i for i in data for n in (0, 1)] # ...

Read More

Dividing two lists in Python

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

Dividing two lists element-wise is a common operation in Python data manipulation. You can achieve this using several approaches including zip() with list comprehension, the operator.truediv function with map(), or NumPy arrays for numerical computations. Using zip() with List Comprehension The zip() function pairs elements from two lists, allowing you to apply division to corresponding elements ? # Given lists numbers1 = [12, 4, 0, 24] numbers2 = [6, 3, 8, -3] print("Given list 1:", numbers1) print("Given list 2:", numbers2) # Use zip with list comprehension result = [i / j for i, j ...

Read More

Difference of two lists including duplicates in Python

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

Sometimes we need to find the differences between two lists while preserving duplicates. This operation is similar to mathematical subtraction where elements from the first list are removed based on their occurrences in the second list. If an element appears multiple times, only the matching count is removed. Using Counter from collections The Counter class from the collections module tracks element frequencies and supports subtraction operations ? from collections import Counter # initializing lists listA = ['Mon', 'Tue', 9, 3, 3] listB = ['Mon', 3] # printing original lists print("Given ListA :", listA) print("Given ...

Read More

Dictionary with index as value in Python

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

In this article we will learn how to create a dictionary from a Python list, where the list values become dictionary keys and their positions become the values. This is useful when you need to map elements to their index positions. Using enumerate with Dictionary Comprehension The enumerate() function adds a counter to each element in the list. We can use it with dictionary comprehension to create a dictionary where list values become keys and their positions become values ? Example days = ['Mon', 'Tue', 'Wed', 'Wed', 11, 11] # Given list print("Given list:", ...

Read More

Dictionary creation using list contents in Python

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

Changing the collection type from one type to another is a very frequent need in Python. In this article we will see how we create a dictionary when multiple lists are given. The challenge is to combine all these lists to create one dictionary accommodating all these values in a dictionary key-value format. Using zip() Function The zip() function can be used to combine the values of different lists. In the below example we have taken three lists as input and combine them to form a single dictionary. One of the lists supplies the keys for the dictionary ...

Read More

Delete items from dictionary while iterating in Python

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

A Python dictionary is a collection which is unordered, changeable and indexed. They have keys and values and each item is referenced using the key. Modifying a dictionary while iterating over it can cause runtime errors, so we need special techniques to safely delete items during iteration. Using del with Collected Keys In this approach we first collect the keys that need to be deleted, then iterate through this separate list to delete the items. This prevents modification of the dictionary during iteration ? Example # Given dictionary days_dict = {1: 'Mon', 2: 'Tue', 3: ...

Read More

Delete elements in range in Python

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

Deleting elements from a Python list by specific indices requires careful handling to avoid index shifting issues. This article explores two effective approaches to delete multiple elements based on their index positions. Using sorted() with del Statement The most straightforward approach is to sort the indices in reverse order and delete elements from highest to lowest index. This prevents index shifting issues ? Example numbers = [11, 6, 8, 3, 2] # The indices to delete indices_to_delete = [1, 3, 0] # printing the original list print("Given list is :", numbers) print("The indices ...

Read More

Custom list split in Python

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

Data analytics throws complex scenarios where the data need to be wrangled and moved around. In this context let's see how we can take a big list and split it into many sublists as per the requirement. In this article we will explore the approaches to achieve this. Using List Comprehension and zip() In this approach we use list slicing to get the elements from specific split points. Then we use zip() to create start and end indices for each sublist. Example data_list = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] ...

Read More

Create list of numbers with given range in Python

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

Python provides several built-in functions and libraries to generate sequences of numbers within a specified range. This article explores different approaches using range(), random.randrange(), and NumPy's arange() function. Using range() Function The range() function generates a sequence of numbers starting from 0 by default, incrementing by 1, and ending before a specified number. You can customize the start, end, and step values to meet your requirements. Example def generate_numbers(start, end, step): return list(range(start, end, step)) # Generate numbers from -3 to 6 with step 2 start, end, step = -3, ...

Read More
Showing 8161–8170 of 8,546 articles
« Prev 1 815 816 817 818 819 855 Next »
Advertisements