Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 877 of 2109
Python - Convert list of nested dictionary into Pandas Dataframe
Many times Python will receive data from various sources which can be in different formats like CSV, JSON etc which can be converted to Python lists or dictionaries. But to apply calculations or analysis using packages like pandas, we need to convert this data into DataFrames. In this article we will see how we can convert a given Python list whose elements are nested dictionaries, into a pandas DataFrame. Basic Approach We first take the list of nested dictionary and extract the rows of data from it. Then we create another for loop to append the rows into ...
Read MoreGet unique values from a list in Python
A list in Python is a collection of items placed within [] which may contain duplicate values. In this article, we will explore different methods to extract only the unique values from a list while preserving or modifying the original order. Using append() with Manual Checking This approach creates a new empty list and appends elements only if they don't already exist. We use a for loop with not in condition to check for duplicates ? Example def get_unique_values(original_list): # Initialize an empty list unique_list = [] ...
Read MoreGet last element of each sublist in Python
A list in Python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the last element of each sublist in a given list. Using for Loop It is a very simple approach in which we loop through the sublists fetching the item at index -1 in them. A for loop is used for this purpose as shown below ? sublists = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:") print(sublists) print("Last Items from sublists:") for item in ...
Read MoreGet first element of each sublist in Python
A list in Python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the first element of each sublist in a given list. Using for Loop It is a very simple approach in which we loop through the sublists fetching the item at index 0 in them. A for loop is used for this purpose as shown below ? Example nested_list = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", nested_list) print("First Items from sublists:") for item ...
Read MoreCount unique sublists within list in Python
A Python list can contain sublists, which are lists nested within a larger list. In this article we will explore how to count the number of unique sublists within a given list using two different approaches. Using Counter Counter is a subclass of Dictionary used to keep track of elements and their count. It stores elements as dictionary keys and their count as dictionary values. To count sublists, we convert each sublist to a string representation ? Example from collections import Counter # Given List with sublists days_list = [['Mon'], ['Tue', 'Wed'], ['Tue', 'Wed']] ...
Read MoreAssign ids to each unique value in a Python list
When working with Python lists, you may need to assign unique IDs to each distinct value while ensuring that duplicate values receive the same ID. This is useful for data analysis, categorization, and indexing operations. Using enumerate() and OrderedDict.fromkeys() The enumerate() function creates a counter for each element, while OrderedDict.fromkeys() preserves the first occurrence order and eliminates duplicates ? from collections import OrderedDict values = ['Mon', 'Tue', 'Wed', 'Mon', 5, 3, 3] print("The given list:", values) # Assigning ids to values list_ids = [{v: k for k, v in enumerate( ...
Read MorePython to Find number of lists in a tuple
A Python tuple is ordered and unchangeable. It can contain lists as its elements. Given a tuple made up of lists, let's find out how many lists are present in the tuple using different approaches. Using len() Function The simplest approach is to use the built-in len() function, which returns the count of elements (lists) in the tuple ? tupA = (['a', 'b', 'x'], [21, 19]) tupB = (['n', 'm'], ['z', 'y', 'x'], [3, 7, 89]) print("The number of lists in tupA:", len(tupA)) print("The number of lists in tupB:", len(tupB)) The output of ...
Read MoreFind missing numbers in a sorted list range in Python
Given a sorted list of numbers, we want to find out which numbers are missing from the continuous range. Python provides several approaches to identify these gaps in the sequence. Using range() with List Comprehension We can create a complete range from the first to last element and check which numbers are not in the original list ? numbers = [1, 5, 6, 7, 11, 14] # Original list print("Given list:", numbers) # Find missing numbers using range missing = [x for x in range(numbers[0], numbers[-1] + 1) ...
Read MoreFind missing elements in List in Python
When working with a list of numbers, you may need to find which values are missing from a contiguous sequence. Python provides several approaches to identify missing elements from 0 to the maximum value in the list. Using List Comprehension with range() and max() The most straightforward approach uses list comprehension to check each number in the range and identify which ones are not in the original list ? numbers = [1, 5, 6, 7, 11, 14] # Original list print("Given list:", numbers) # Find missing elements using list comprehension missing = [num for ...
Read MoreFind mismatch item on same index in two list in Python
Sometimes we need to compare elements in two Python lists based on both their value and position. This article shows different methods to find indices where elements at the same position have different values. Using for Loop We can design a for loop to compare values at similar indexes. If the values do not match, we add the index to a result list ? listA = [13, 'Mon', 23, 62, 'Sun'] listB = [5, 'Mon', 23, 6, 'Sun'] # Index variable idx = 0 # Result list res = [] # With iteration ...
Read More