Found 10784 Articles for Python

Find sum of frequency of given elements in the list in Python

Pradeep Elance
Updated on 05-May-2020 10:21:16

404 Views

A given list has many repeated items. We are interested in finding out the sum of the frequency of some such items which are repeated in the list. Below are the approaches how we can achieve this.With sumWe have two lists. One has the list of values and other has the values whose frequency needs to be checked from the first list. So we create a for loop to count the number of occurrences of the elements from the second list in the first list and then apply the sum function to get the final sum of frequency.Example Live Demochk_list= ['Mon', ... Read More

Find frequency of given character at every position in list of lists in Python

Pradeep Elance
Updated on 05-May-2020 10:19:58

177 Views

Lets consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement.Consider a list of lists given below.listA = [['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']]In the abobe list we have elements which are lists with 3 elements. If I consider the first inner list tit has a, a, b at positions 0, 1, 2. Similarly for the 3rd list it is c, a, b at 0, ... Read More

Find elements of a list by indices in Python

Pradeep Elance
Updated on 05-May-2020 10:17:19

5K+ Views

Consider two lists. The elements in the second list are numbers which needs to be considered as index position for elements of the first list. For this scenario we have the below python programs.With map and getitemWe can use the getitem magic method is used to access the list items. We can use it along with the map function, so that we get the result from first list which takes the elements from second list as its indics.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1, 3] print("Given list A:", listA) print("Given list B:", listB) ... Read More

Extract only characters from given string in Python

Pradeep Elance
Updated on 05-May-2020 10:16:10

3K+ Views

A piece of data may contain letters, numbers as well as special characters. If we are interested in extracting only the letters form this string of data, then we can use various options available in python.With isalphaThe isalpha function will check if the given character is an alphabet or not. We will use this inside a for loop which will fetch each character from the given string and check if it is an alphabet. The join method will capture only the valid characters into the result.Example Live DemostringA = "Qwer34^&t%y" # Given string print("Given string : ", stringA) # ... Read More

Extract numbers from list of strings in Python

Pradeep Elance
Updated on 05-May-2020 10:14:34

1K+ Views

While using python for data manipulation, we may come across lists whose elements are a mix of letters and numbers with a fixed pattern. In this article we will see how to separate the numbers form letters which can be used for future calculations.With splitThe split functions splits a string by help of a character that is treated as a separator. In the program below the list elements have hyphen as their separator between letters and text. We will use that along with a for loop to capture eachExample Live DemolistA = ['Mon-2', 'Wed-8', 'Thu-2', 'Fri-7'] # Given list print("Given ... Read More

Equate two list index elements in Python

Pradeep Elance
Updated on 05-May-2020 10:13:02

474 Views

During data manipulation with Python , we may need to bring two lists together and equate the elements in each of them pair wise. Which means the element at index 0 from list 1 will be equated with element from index 0 of list2 and so on.With tupleThe tuple function will be leveraged to take elements form each list in sequence and matching them up. We first store the result in a temp string which has the pattern in which the output of matching up of the values form lists will be displayed.Example Live DemolistA = ['day1', 'day2', 'day3'] listB = ... Read More

Element with largest frequency in list in Python

Pradeep Elance
Updated on 05-May-2020 10:09:02

315 Views

A lot of statistical data analysis tries to find the values which have maximum frequency in a given list of values. Python provides multiple approaches using which we can find such value form a given list. Below are the approaches.Using CounterThe Counter function from collections module has a options which can directly find the most common element in a given list. We have the most_common function to which we pass a parameter 1 for only one element with highest frequency and pass 2 if we need two elements which have highest frequency.Example Live Demofrom collections import Counter # Given list ... Read More

Element repetition in list in Python

Pradeep Elance
Updated on 05-May-2020 10:08:10

411 Views

There are scenarios when we need to repeat the values in a list. This duplication of values can be achived in python in the following ways.Using nested for loopIt is a straight forward approach in which pick each element, take through a inner for loop to create its duplicate and then pass both of them to an outer for loop.Example Live Demo# Given list listA = ['Mon', 'Tue', 9, 3, 3] print("Given list : ", listA) # Adding another element for each element Newlist = [i for i in listA for n in (0, 1)] # Result print("New ... Read More

Dividing two lists in Python

Pradeep Elance
Updated on 05-May-2020 10:06:59

1K+ Views

The elements in tow lists can be involved in a division operation for some data manipulation activity using python. In this article we will see how this can be achieved.With zipThe zip function can pair up the two given lists element wise. The we apply the division mathematical operator to each pair of these elements. Storing the result into a new list.Example Live Demo# Given lists list1 = [12, 4, 0, 24] list2 = [6, 3, 8, -3] # Given lists print("Given list 1 : " + str(list1)) print("Given list 2 : " + str(list2)) # Use zip res ... Read More

Difference of two lists including duplicates in Python

Pradeep Elance
Updated on 04-May-2020 13:11:55

240 Views

Sometimes we need to find the differences between two lists. It will also mean a mathematical subtraction in which the elements from the first list are removed if they are present in the second list. Duplicates are preserved. Below is the approach through which we can achieve this.We can use the Counter method from the collections module which will keep track of the count of elements. A straight mathematical subtraction gives the desired result. In the final result the number of occurrences of an element between the first and the second list will decide the elements.Example Live Demofrom collections import Counter ... Read More

Advertisements