Found 10784 Articles for Python

Find common elements in list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:54:18

2K+ Views

It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this.With map and intersectionIntersection is a simple mathematical concept of finding the common elements between different sets. Python has the set method which returns a set that contains the similarity between two or more sets. So we first convert the elements of the list into set through a map function and then apply the ... Read More

Convert list into list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:52:01

3K+ Views

During data analysis we face scenarios to convert every element of a list into a sublist. So in this article we will need to take a normal list as an input and convert into a list of lists where each element becomes a sublist.Using for loopThis is a very straight forward approach in which we create for loop to read each element. We read it as a list and store the result in the new list.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] #Given list print("Given list: ", Alist) # Each element as list NewList= [[x] for x ... Read More

Convert dictionary to list of tuples in Python

Pradeep Elance
Updated on 13-May-2020 14:49:30

668 Views

Converting from one collection type to another is very common in python. Depending the data processing needs we may have to convert the key value pairs present in a dictionary to pairs representing tuples in a list. In this article we will see the approaches to achieve this.With inThis is a straight forward approach where we just consider theExample Live DemoAdict = {30:'Mon', 11:'Tue', 19:'Fri'} # Given dictionary print("The given dictionary: ", Adict) # Using in Alist = [(key, val) for key, val in Adict.items()] # Result print("The list of tuples: ", Alist)OutputRunning the above code gives us ... Read More

Check if two lists are identical in Python

Pradeep Elance
Updated on 13-May-2020 14:47:00

444 Views

In python data analysis, we may come across situation when we need to compare two lists and find out if they are identical meaning having same elements or not.Exmple Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu'] listB = ['Mon', 'Wed', 'Tue', 'Thu'] # Given lists print("Given listA: ", listA) print("Given listB: ", listB) # Sort the lists listA.sort() listB.sort() # Check for equality if listA == listB:    print("Lists are identical") else:    print("Lists are not identical")OutputRunning the above code gives us the following result −Given listA: ['Mon', 'Tue', 'Wed', 'Thu'] Given listB: ['Mon', 'Wed', 'Tue', 'Thu'] Lists are identicalWith ... Read More

Check if substring present in string in Python

Pradeep Elance
Updated on 13-May-2020 14:44:33

173 Views

In python data analysis we may come across a scenario to check if a given substring is part of a bigger string. We will achieve this through the following programs.With findThe find function finds the first occurrence of the specified value. If the value is not found then it returns -1. We will apply this function to the given string and design a if clause to find out is substring is part of a string.Example Live DemoAstring = "In cloud 9" Asub_str = "cloud" # Given string and substring print("Given string: ", Astring) print("Given substring: ", Asub_str) if (Astring.find(Asub_str) == -1): ... Read More

Check if one list is subset of other in Python

Pradeep Elance
Updated on 13-May-2020 14:41:53

4K+ Views

In text analytics and various other fields of data analytics it is often needed to find if a given list is already a part of a bigger list. In this article we will see the python programs to implement this requirement.With allWe use a for loop to check if every element of the smaller list is present in the bigger list. The all function ensures each evaluation returns true.Example Live DemoAlist = ['Mon', 'Tue', 5, 'Sat', 9] Asub_list = ['Tue', 5, 9] # Given list and sublist print("Given list ", Alist) print("Given sublist", Asub_list) # With all if (all(x ... Read More

Check if list is strictly increasing in Python

Pradeep Elance
Updated on 13-May-2020 14:39:04

952 Views

Given a list, we may need to check for the sequence of its elements. In this article we will find out if the elements present in the list are in a strictly increasing order. Below programs achieve that objective.With all and zipIn this approach we first slice each element compare its value to the next element that is sliced. If all such comparisons hold true then we conclude the list is strictly in increasing order.Example Live DemolistA = [11, 23, 42, 51, 67] #Given list print("Given list : ", listA) # Apply all and range if (all(i < j for i, ... Read More

Check if list is sorted or not in Python

Pradeep Elance
Updated on 13-May-2020 14:35:11

4K+ Views

Lists are the most widely used data collectios in python. We may come across situation when we need to know if the given list is already sorted or not. In this article we will see the approaches to achieve this.With sortWe take a copy of the given list, apply sort function to it and store that copy as a new list. Then we compare it with the original list and check if they are equal or not.Example Live DemolistA = [11, 23, 42, 51, 67] #Given list print("Given list : ", listA) listA_copy = listA[:] # Apply sort to copy listA_copy.sort() ... Read More

Check if list contains consecutive numbers in Python

Pradeep Elance
Updated on 13-May-2020 14:32:52

3K+ Views

Depending on the needs of our data analysis we may need to check for presence of sequential numbers in a python data container. In the below programs we find out if among the elements of Alist, there are any consecutive numbers.With range and sortedThe sorted function will rearrange the elements of the list in a sorted order. We then apply the range function taking the lowest and highest numbers form the list using min and max functions. We store the results of above operations in two lists and compare them for equality.Example Live DemolistA = [23, 20, 22, 21, 24] sorted_list ... Read More

Check if given string can be formed by concatenating string elements of list in Python

Pradeep Elance
Updated on 13-May-2020 14:30:47

475 Views

We sometimes need to check if a required string can be formed from many number of strings that are present in a list. It also should not matter in what order the strings are present in the list which have to be joined to get the required string.With permutationsFrom itertools we can use the permutations function which will give us the possible combinations of the strings in the list in various order. As soon as a given combination matches the required string, we conclude that the string can be formed.Example Live Demofrom itertools import permutations chk_str = 'balloon' Alist = ... Read More

Advertisements