Found 10476 Articles for Python

Finding relative order of elements in list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:58:49

477 Views

We are given a list whose elements are integers. We are required to find the relative order which means if they are sorted in ascending order then we need to find index of their positions.With sorted and indexWe first sort the entire list and then find out the index of each of them after the sorting.Example Live DemolistA = [78, 14, 0, 11] # printing original list print("Given list is : ", listA) # using sorted() and index() res = [sorted(listA).index(i) for i in listA] # printing result print("list with relative ordering of elements : ", res)OutputRunning the above code gives ... Read More

Find whether all tuple have same length in Python

Pradeep Elance
Updated on 04-Jun-2020 11:57:20

262 Views

In this article we will find out if all the tuples in a given list are of same length.With lenWe will use len function and compare its result to a given value which we are validating. If the values are equal then we consider them as same length else not.Example Live DemolistA = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')] # printing print("Given list of tuples:", listA) # check length k = 3 res = 1 # Iteration for tuple in listA:    if len(tuple) != k:       res = 0       break # Checking if ... Read More

Find top K frequent elements from a list of tuples in Python

Pradeep Elance
Updated on 04-Jun-2020 11:55:30

225 Views

We have a list of tuples. In it we are required to find the top k frequent element. If k is 3 we are required to find the top three elements from the tuples inside the list.With defaultdictWe put the the elements into a dictionary container using defaultdict. Then find out only the elements which satisfy that top k conditions.Example Live Demoimport collections from operator import itemgetter from itertools import chain # Input list initialization listA = [[('Mon', 126)], [('Tue', 768)], [('Wed', 512)], [('Thu', 13)], [('Fri', 341)]] # set K K = 3 #Given list print("Given list:", listA) print("Check value:", K) ... Read More

Find the tuples containing the given element from a list of tuples in Python

Pradeep Elance
Updated on 04-Jun-2020 11:52:23

689 Views

A list can have tuples as its elements. In this article we will learn how to identify those tuples which contain a specific search element which is a string.With in and conditionWe can design a follow with in condition. After in we can mention the condition or a combination of conditions.Example Live DemolistA = [('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)] test_elem = 'Mon' #Given list print("Given list:", listA) print("Check value:", test_elem) # Uisng for and if res = [item for item in listA    if item[0] == test_elem and item[1] >= 2] # printing res print("The tuples satisfying the ... Read More

Find the sublist with maximum value in given nested list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:50:29

655 Views

A list can contain other list as its elements. In this article we are equal to find the sublist with maximum value which are present in a given list.With max and lambdaThe max and the Lambda function can together be used to give that sublist which has the maximum value.Example Live DemolistA = [['Mon', 90], ['Tue', 32], ['Wed', 120]] # Using lambda res = max(listA, key=lambda x: x[1]) # printing output print("Given List:", listA) print("List with maximum value: ", res)OutputRunning the above code gives us the following result −Given List: [['Mon', 90], ['Tue', 32], ['Wed', 120]] List with maximum value: ['Wed', ... Read More

Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python

Pradeep Elance
Updated on 04-Jun-2020 11:48:05

329 Views

In this article we are required to find that number from the list which occurs odd number of times in the given list. We are also required to use the Lambda function and the reduce function.We design a function where the reduce function is used by applying the Lambda function to check if the element is present odd number of times.Example Live Demofrom functools import reduce def oddcount(i):    print(reduce(lambda x, y: x ^ y, i)) listA = [12, 34, 12, 12, 34] print("Given list:", listA) print("The element present odd number of times:") oddcount(listA)OutputRunning the above code gives us the following ... Read More

Find the list elements starting with specific letter in Python

Pradeep Elance
Updated on 04-Jun-2020 11:46:22

3K+ Views

In this article we will find all those elements from a list which start with specific letter.With index and lowerWe use the lower function so that the test later can match with the first letter of the the elements in the list irrespective of the case. Then we use the index at 0 so that the first letter of the elements in a list are compared with the test letter.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu'] # Test with letter test = 'T' # printing original list print("Given list " ,listA) # using lower and idx res = [idx for ... Read More

Find most frequent element in a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:44:36

8K+ Views

In this article we will see how to find the element which is most common in a given list. In other words, the element with highest frequency.With max and countWe apply why the set function to get the unique elements of the list and then keep account of each of those elements in the list. Finally apply a max function to get the element with highest frequency.Example Live Demo# Given list listA = [45, 20, 11, 50, 17, 45, 50, 13, 45] print("Given List:", listA) res = max(set(listA), key = listA.count) print("Element with highest frequency:", res)OutputRunning the above code gives us ... Read More

Find most common element in a 2D list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:42:24

665 Views

A 2D list has list as its element. In other words it is a list of lists. In this article we are required to find the element which is most common among all the lists inside a list.With max and countWe design a follow with a in condition to check for the presence of an element in a given sublist. Then we apply the max function along with the count function to get the the element with maximum frequency.Example Live Demodef highest_freq(lst):    SimpleList = [el for sublist in lst for el in sublist]    return max( SimpleList, key= SimpleList.count) # ... Read More

Find maximum value in each sublist in Python

Pradeep Elance
Updated on 04-Jun-2020 11:40:56

380 Views

We are given a list of lists. In the inner lists or sublists we are required to find the maximum value in each.With max and inWe design a for loop with in condition and apply the max function to get the maximum value in each of the sublist.Example Live DemoAlist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]] # Given list print("The given list: ", Alist) # Use Max res = [max(elem) for elem in Alist] # Printing max print("Maximum values from each element in the list: ", res)OutputRunning the above code gives us the following result −The given ... Read More

Advertisements