Found 10783 Articles for Python

Find missing elements in List in Python

Pradeep Elance
Updated on 26-Aug-2020 08:08:57

1K+ Views

If we have a list containing numbers, we can check if the numbers are contiguous or not and also find which numbers are missing from a range of numbers considering the highest number as the final value.With range and maxWe can design a for loop to check for absence of values in a range using the not in operator. Then capture all these values by adding them to a new list which becomes the result set.Example Live DemolistA = [1, 5, 6, 7, 11, 14] # Original list print("Given list : ", listA) # using range and max res ... Read More

Find mismatch item on same index in two list in Python

Pradeep Elance
Updated on 26-Aug-2020 08:07:12

3K+ Views

Sometimes we may need to compare elements in two python lists in terms of both their value and position or index. In this article we will see how to find out the elements in two lists at the same position which do not match in their value.Using for loopwe can design for loop to compare the values at similar indexes. Id the values do not match then we add the index to a result list. The for loop first fetches the value in the first index and then uses an if condition to compare it with values from the second ... Read More

Find Min-Max in heterogeneous list in Python

Pradeep Elance
Updated on 26-Aug-2020 08:05:29

434 Views

A python list can contain both strings and numbers. We call it a heterogeneous list. In this article we will take such a list and find the minimum and maximum number present in the list.Finding MinimumIn this approach we will take the isinstance function to find only the integers present in the list and then apply the min function to get the minimum value out of it.Example Live DemolistA = [12, 'Sun', 39, 5, 'Wed', 'Thus'] # Given list print("The Given list : ", listA) res = min(i for i in listA if isinstance(i, int)) # Result print("The ... Read More

Find Mean of a List of Numpy Array in Python

Pradeep Elance
Updated on 26-Aug-2020 08:04:18

663 Views

Numpy is a very powerful python library for numerical data processing. It mostly takes in the data in form of arrays and applies various functions including statistical functions to get the result out of the array. In this article we will see how to get the mean value of a given array.with meanThe mean function can take in an array and give the mathematical mean value of all the elements in it. So we design a for loop to keep track of the length of the input and go through each array calculating its mean.Example Live Demoimport numpy as np ... Read More

Find Maximum difference pair in Python

Pradeep Elance
Updated on 26-Aug-2020 08:02:34

357 Views

Data analysis can throw a variety of challenges. In this article we will take a list with numbers as its elements. Then we will find such pairs of elements in the list which has maximum difference in value between them.With nlargestThe approach here is to first find out all possible combinations of elements and then subtract the second element from the first. Finally apply the nlargest function form heapq module to get those pairs where the difference is maximum.Example Live Demofrom itertools import combinations from heapq import nlargest listA = [21, 14, 30, 11, 17, 18] # Given list ... Read More

Find longest consecutive letter and digit substring in Python

Pradeep Elance
Updated on 26-Aug-2020 08:00:29

279 Views

A given string may be a mixture of digits and letters. In this article we are required to find the biggest substring that has letters and digits together.with re moduleThe regular expression module can be used to find all the continuous substring having digits or letters. Then we apply the max function to pick only those continuous substrings of letters and digits which have maximum length among all the substrings found. The findall function is also use to identify and get the required substrings.Example Live Demoimport re def longSubstring(str):    letter = max(re.findall(r'\D+', str), key=len)    digit = max(re.findall(r'\d+', str), ... Read More

Find keys with duplicate values in dictionary in Python

Pradeep Elance
Updated on 26-Aug-2020 07:57:07

4K+ Views

While dealing with dictionaries, we may come across situation when there are duplicate values in the dictionary while obviously the keys remain unique. In this article we will see how we can achieve thi.Exchanging keys and valuesWe exchange the keys with values of the dictionaries and then keep appending the values associated with a given key. This way the duplicate values get clubbed and we can see them in the resulting new dictionary.Example Live DemodictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3} print("Given Dictionary :", dictA) k_v_exchanged = {} for key, value in dictA.items():    if ... Read More

Find k longest words in given list in Python

Pradeep Elance
Updated on 26-Aug-2020 07:55:36

152 Views

We have a scenario where we have to pick the top n longest word from a list containing many words of varying length. In this article we will see various approaches to achieve that.With count() and sorted()We first sort the elements of the list in the reverse order so that the longest words are available at the beginning of the list. Then find the length of each word and add the result of the count to a variable. Finally take a slice of the required number of longest words we need.Example Live Demofrom itertools import count def longwords(l, x):   ... Read More

Find indices with None values in given list in Python

Pradeep Elance
Updated on 26-Aug-2020 07:51:16

547 Views

Manytime when dealing with data analysis we may come across the None values present in a list. These values can not be used directly in mathematical operations and string operations etc. So we need to find their position and either convert them or use them effectively.With range()Combining the range and len function we can compare the value of each element with None and capture their index positions. Of course we use a for loop design to achieve this.Example Live DemolistA = ['Sun', 'Mon', None, 'Wed', None, None] # Given list print("Given list : ", listA) # Using range positions ... Read More

Find fibonacci series upto n using lambda in Python

Pradeep Elance
Updated on 26-Aug-2020 07:43:29

2K+ Views

A fibinacci series is a widley known mathematical series that explains many natural phenomenon. It starts with 0 and 1 and then goes on adding a term to its previous term to get the next term. In this article we will see how to generate a given number of elements of the Fibonacci series by using the lambda function in python.With sum and mapWe use the map function to apply the lambda function to each element of the list. We design a list slicing mechanism to get sum of previous two terms and use range to keep count of how ... Read More

Advertisements