Server Side Programming Articles

Page 888 of 2109

Combining values from dictionary of list in Python

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

When working with a Python dictionary that has lists as values, you may need to create all possible combinations of keys and their corresponding values. This is useful for generating test data, configuration combinations, or exploring different scenarios. Using sorted() and product() The product() function from itertools creates a Cartesian product of iterables. By sorting the dictionary keys first, we ensure consistent ordering in our combinations ? import itertools as it schedule_dict = { "Day": ["Tue", "Wed"], "Time": ["2pm", "9am"], } # Sorting dictionary keys for ...

Read More

Combining two sorted lists in Python

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

Lists are one of the most extensively used Python data structures. In this article we will see how to combine the elements of two lists and produce the final output in a sorted manner. Using + Operator with sorted() The + operator can join the elements of two lists into one. Then we apply the sorted function which will sort the elements of the final list created with this combination ? listA = ['Mon', 'Tue', 'Fri'] listB = ['Thu', 'Fri', 'Sat'] # Given lists print("Given list A is :", listA) print("Given list B is :", ...

Read More

Combining tuples in list of tuples in Python

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

For data analysis, we sometimes need to combine different data structures available in Python. A list can contain tuples as its elements. In this article we will see how we can combine each element of a list within a tuple with another element from the same tuple to produce new tuple combinations. Understanding the Problem Given a list of tuples where each tuple contains a list and another element, we want to create new tuples by pairing each element from the list with the other element in the original tuple. # Original structure: [([list_elements], 'other_element'), ...] ...

Read More

Checking triangular inequality on list of lists in Python

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

The triangle inequality theorem states that the sum of any two sides of a triangle must be greater than the third side. In Python, we can check this property on a list of lists to identify which sublists can form valid triangles. Using For Loop and Comparison First, we sort each sublist to arrange sides in ascending order. Then we check if the sum of the two smaller sides is greater than the largest side ? Example triangle_data = [[3, 8, 3], [9, 8, 6], [2, 3, 8]] # Sorting sublists to get sides ...

Read More

Checking if starting digits are similar in list in Python

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

Sometimes in a given Python list we may be interested only in the first digit of each element in the list. In this article we will check if the first digit of all the elements in a list are same or not. Using set() with map() Set in Python does not allow any duplicate values in it. So we take the first digit of every element and put it in a set. If all the digits are same then the length of the set will be only 1 as no duplicates are allowed. Example numbers ...

Read More

Check whether a string is valid JSON or not in Python

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

JSON is a text format used to exchange data easily between various computer programs. It has a specific format which Python can validate. In this article, we will consider a string and use the JSON module to validate if the string represents a valid JSON format or not. Using json.loads() Method The json module has a method called loads() that loads a valid JSON string to create a JSON object. We can use a try-except block to catch any parsing errors ? import json def is_valid_json(json_string): try: ...

Read More

Find common elements in list of lists in Python

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

When working with nested lists, you may need to find elements that appear in all inner lists. Python provides several approaches to find common elements across multiple lists using set operations. Using map() and set.intersection() The most straightforward approach uses set intersection. We convert each inner list to a set and find their intersection ? nested_lists = [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ...

Read More

Convert dictionary to list of tuples in Python

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

Converting from one collection type to another is very common in Python. Depending on the data processing needs we may have to convert the key-value pairs present in a dictionary to tuples in a list. In this article we will see the approaches to achieve this. Using Dictionary items() This is the most straightforward approach where we use list comprehension with the items() method to extract key-value pairs as tuples ? Example day_dict = {30: 'Mon', 11: 'Tue', 19: 'Fri'} # Given dictionary print("The given dictionary:", day_dict) # Using list comprehension with items() ...

Read More

Check if two lists are identical in Python

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

In Python data analysis, we may come across situations when we need to compare two lists and find out if they are identical, meaning having the same elements regardless of order. Python provides several methods to achieve this comparison. Using Sorting Method The simplest approach is to sort both lists and then compare them for equality. This method works when you want to check if both lists contain the same elements ? days_a = ['Mon', 'Tue', 'Wed', 'Thu'] days_b = ['Mon', 'Wed', 'Tue', 'Thu'] # Given lists print("Given days_a:", days_a) print("Given days_b:", days_b) # ...

Read More

Check if list is strictly increasing in Python

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

A strictly increasing list is one where each element is smaller than the next element. Python provides several approaches to check this condition using all() with zip(), comparison operators, or the itertools module. Using all() and zip() This approach compares each element with its next element using zip(). The all() function returns True only if all comparisons are True ? numbers = [11, 23, 42, 51, 67] print("Given list:", numbers) # Check if strictly increasing if all(i < j for i, j in zip(numbers, numbers[1:])): print("Yes, List is strictly increasing.") else: ...

Read More
Showing 8871–8880 of 21,090 articles
« Prev 1 886 887 888 889 890 2109 Next »
Advertisements