Python Articles

Page 814 of 855

Convert a string representation of list into list in Python

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

In Python, you may encounter situations where a list is stored as a string representation. This commonly happens when reading data from files, APIs, or user input. Python provides several methods to convert these string representations back into actual list objects. Using strip() and split() This method manually removes the square brackets and splits the string by commas. It works well for simple cases but treats all elements as strings ? string_data = "[Mon, 2, Tue, 5]" # Given string print("Given string:", string_data) print("Type:", type(string_data)) # Convert string to list result = string_data.strip('[]').split(', ') ...

Read More

Convert a list of multiple integers into a single integer in Python

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

Sometimes we may have a list whose elements are integers. There may be a need to combine all these elements and create a single integer out of it. In this article we will explore the ways to do that. Using List Comprehension with join() The join() method can join all items in a list into a string. We use list comprehension to convert each integer to a string, then join them together ? Example numbers = [22, 11, 34] # Given list print("Given list:", numbers) # Convert each integer to string and join ...

Read More

Consecutive elements pairing in list in Python

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

During data analysis using Python, we may need to pair up consecutive elements of a list. This creates overlapping pairs where each element (except the first and last) appears in two pairs. Here are the most common approaches to achieve this. Using List Comprehension with range() We can use list comprehension with range() to iterate through consecutive indexes and pair adjacent elements ? numbers = [51, 23, 11, 45] # Given list print("Given list:", numbers) # Pair consecutive elements using list comprehension result = [[numbers[i], numbers[i + 1]] for i in range(len(numbers) - 1)] ...

Read More

Consecutive element maximum product in Python

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

Python provides several approaches to find the maximum product of two consecutive digits in a string. This is useful when processing numerical data stored as strings and finding optimal adjacent pairs. Using zip() and max() We can create pairs of consecutive elements using zip() with list slicing, then find the maximum product ? number_string = '5238521' # Given string print("Given String:", number_string) # Convert to list for easier processing digits_list = list(number_string) print("String converted to list:", digits_list) # Using zip() to create consecutive pairs and max() to find maximum product result = max(int(a) ...

Read More

Concatenate two lists element-wise in Python

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

Python has great data manipulation features. In this article we will see how to combine the elements from two lists in the same order as they are present in the lists. Using zip() with List Comprehension The zip() function pairs elements from two lists, allowing us to concatenate them element-wise. We can use list comprehension to create a new list with concatenated elements ? list_a = ["Outer-", "Frost-", "Sun-"] list_b = ['Space', 'bite', 'rise'] # Given lists print("Given list A:", list_a) print("Given list B:", list_b) # Use zip with list comprehension result = [i ...

Read More

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
Showing 8131–8140 of 8,546 articles
« Prev 1 812 813 814 815 816 855 Next »
Advertisements