Found 10784 Articles for Python

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

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

545 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

534 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

223 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

2K+ 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

7K+ 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

527 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

293 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

Count tuples occurrence in list of tuples in Python

Pradeep Elance
Updated on 04-Jun-2020 11:39:00

403 Views

A list is made up of tuples as its element. In this article we will count the number of unique tuples present in the list.With defaultdictWe treat the given list as a defaultdict data container and count the elements in it using the in condition.Example Live Demoimport collections Alist = [[('Mon', 'Wed')], [('Mon')], [('Tue')], [('Mon', 'Wed')] ] # Given list print("Given list:", Alist) res = collections.defaultdict(int) for elem in Alist:    res[elem[0]] += 1 print("Count of tuples present in the list:", res)OutputRunning the above code gives us the following result −Given list: [[('Mon', 'Wed')], ['Mon'], ['Tue'], [('Mon', 'Wed')]] Count of tuples ... Read More

Count the sublists containing given element in a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:37:17

331 Views

The elements in a given list can also be present as another string in another variable. In this article we'll see how many times a given stream is present in a given list.With range and lenWe use the range and len function to keep track of the length of the list. Then use the in condition to find the number of times the string is present as an element in a list. A count variable initialized to zero keeps incrementing whenever the condition is met.Example Live DemoAlist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] Bstring = 'Mon' # Given list print("Given ... Read More

Count the Number of matching characters in a pair of string in Python

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

718 Views

We are given two strings. We need to find the count it of the characters in the first string which are also present in a second string.With setThe set function gives us unique values all the elements in a string. We also use the the & operator which finds the common elements between the two given strings.Example Live DemostrA = 'Tutorials Point' uniq_strA = set(strA) # Given String print("Given String", strA) strB = 'aeio' uniq_strB = set(strB) # Given String print("Search character strings", strB) common_chars = uniq_strA & uniq_strB print("Count of matching characters are : ", len(common_chars))OutputRunning the above code gives ... Read More

Advertisements