Convert a Nested List into a Flat List in Python

Pradeep Elance
Updated on 20-May-2020 10:41:15

694 Views

A nested list is a list whose elements are lists themselves. If we have a python data container which is a nested list, we may sometimes need to convert it into a flattened list so that each element can be processed further.Even the inner elements can also be nested themselves. And there can be many layers of nesting. So we will approach this problem with recursion. We will keep checking if the element is nested and then keep going applying the function again and again until the element is no longer a list. Once it is found that element is ... Read More

Convert List of Integers into a Single Integer in Python

Pradeep Elance
Updated on 20-May-2020 10:38:55

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.With joinThe join method can Join all items in a tuple into a string. So we will use it to join each element of the list by iterating through them through a for loop.Example Live DemolistA = [22, 11, 34] # Given list print("Given list A: ", listA) # Use res = int("".join([str(i) for i in listA])) # Result print("The integer is : ... Read More

Convert a List into Tuple of Lists in Python

Pradeep Elance
Updated on 20-May-2020 10:37:15

580 Views

Converting one data container into another in python is a frequent requirement. In this article we will take a list and convert into a tuple where each element of the tuple is also a list.With tupleWe can apply the tuple function straight to the list. But we have to also put a for loop in place so that each element is enclosed in a [].Example Live DemolistA = ["Mon", 2, "Tue", 3] # Given list print("Given list A: ", listA) # Use zip res = tuple([i] for i in listA) # Result print("The tuple is : ", res)OutputRunning the above code ... Read More

Consecutive Elements Pairing in List in Python

Pradeep Elance
Updated on 20-May-2020 10:34:18

765 Views

During data analysis using python, we may come across a need to pair-up the consecutive elements of a list. In this article we will see the various ways to achieve this.With index and rangeWe will design an expression to put the consecutive indexes of the list elements together. And then apply the range function to determine the maximum number of times this pairing of consecutive elements will go on.Example Live DemolistA = [51, 23, 11, 45] # Given list print("Given list A: ", listA) # Use res = [[listA[i], listA[i + 1]]    for i in range(len(listA) - 1)] # Result ... Read More

Consecutive Element Maximum Product in Python

Pradeep Elance
Updated on 20-May-2020 10:31:06

337 Views

Python has great libraries to manipulate data. We may come across a need to find the maximum product of two consecutive numbers which are part of the big string. In this article we will see the ways to achieve that.With zip and maxWe convert the string into a list. Then create pairs from the consecutive elements with help of slicing. Applying * we multiply the pair and then take the max value from the result of the multiplication from each of the pairs.Example Live DemoAstring = '5238521' # Given string print("Given String : ", Astring) # Convert to list Astring = ... Read More

Concatenate Two Lists Element-wise in Python

Pradeep Elance
Updated on 20-May-2020 10:28:39

1K+ Views

Pyhton 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.With zipThe zip function can take in the two lists as parameters and concatenate them. We design a for loop to capture these combinations and put them into a new list.Example Live DemolistA = ["Outer-", "Frost-", "Sun-"] listB = ['Space', 'bite', 'rise'] # Given lists print("Given list A: ", listA) print("Given list B: ", listB) # Use zip res = [i + j for i, j in zip(listA, listB)] # Result ... Read More

Combining Values from Dictionary of List in Python

Pradeep Elance
Updated on 20-May-2020 10:26:12

578 Views

Let’s say we have a python dictionary which has lists as it values in the key value pairs. We need to create a list which will represent all possible combinations of the keys and values from the given lists.With sorted and productThe product function from itertools can be used to create a crtesian product of the iterable supplied to it as parameter. We sort the dictionary and use two for loops to create the combination of all possible key value pairs from the lists in the dictionary.Example Live Demoimport itertools as it Adict = {    "Day": ["Tue", "Wed"],    "Time": ... Read More

Combine Two Sorted Lists in Python

Pradeep Elance
Updated on 20-May-2020 10:21:37

354 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.With + and sortedThe + 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.Example Live DemolistA = ['Mon', 'Tue', 'Fri'] listB = ['Thu', 'Fri', 'Sat'] # Given lists print("Given list A is : ", listA) print("Given list B is : ", listB) # Add and sort res = ... Read More

Combine Tuples in List of Tuples in Python

Pradeep Elance
Updated on 20-May-2020 10:20:36

607 Views

For data analysis, we sometimes take a combination of 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 tuple with another given element and produce a list tuple combination.With for loopIn the below approach we create for loops that will create a pair of elements by taking each element of the tuple and looping through the element in the list.Example Live DemoAlist = [([2, 8, 9], 'Mon'), ([7, 5, 6], 'Wed')] # Given list of tuple print("List of tuples : ", Alist) # ... Read More

Check Triangular Inequality on List of Lists in Python

Pradeep Elance
Updated on 20-May-2020 10:15:50

346 Views

The sum of two sides of a triangle is always greater than the third side. This is called triangle inequality. Python list of lists we will identify those sublists where the triangle inequality holds good.With for and >We will first get all the sublists sorted. Then for each sublist we will check if the if the sum of first two elements is greater than the third element.Example Live DemoAlist = [[3, 8, 3], [9, 8, 6]] # Sorting sublist of list of list for x in Alist:    x.sort() # Check for triangular inequality for e in Alist:    if e[0] ... Read More

Advertisements