Pradeep Elance

Pradeep Elance

302 Articles Published

Articles by Pradeep Elance

Page 16 of 31

Checking if starting digits are similar in list in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 408 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.With set and mapSet 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 has no duplicates allowed.Example Live DemoAlist = [63, 652, 611, 60] # Given list print("Given ...

Read More

Check whether a string is valid JSON or not in Python

Pradeep Elance
Pradeep Elance
Updated on 20-May-2020 2K+ Views

JSON is a type of text format use 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 using JSON module we will validate if the string represents a valid JSON format or not.Creating JSON ObjectThe json module has method called loads. It loads a valid json string to create a Json object. In this example we load the string and check that there is no error in loading the JSON object. If there is error we consider the JSON string as invalid.Example Live Demoimport ...

Read More

Find common elements in list of lists in Python

Pradeep Elance
Pradeep Elance
Updated on 13-May-2020 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 dictionary to list of tuples in Python

Pradeep Elance
Pradeep Elance
Updated on 13-May-2020 927 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
Pradeep Elance
Updated on 13-May-2020 707 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 list is strictly increasing in Python

Pradeep Elance
Pradeep Elance
Updated on 13-May-2020 1K+ 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
Pradeep Elance
Updated on 13-May-2020 6K+ 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
Pradeep Elance
Updated on 13-May-2020 4K+ 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
Pradeep Elance
Updated on 13-May-2020 619 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

Check if given multiple keys exist in a dictionary in Python

Pradeep Elance
Pradeep Elance
Updated on 13-May-2020 2K+ Views

During data analysis using python, we may need to verify if a couple of values exist as keys in a dictionary. So that the next part of the analysis can only be used with the keys that are part of the given values. In this article we will see how this can be achieved.With Comparison operatorsThe values to be checked are put into a set. Then the content of the set is compared with the set of keys of the dictionary. The >= symbol indicates all the keys from the dictionary are present in the given set of values.Example Live DemoAdict ...

Read More
Showing 151–160 of 302 articles
« Prev 1 14 15 16 17 18 31 Next »
Advertisements