Pradeep Elance

Pradeep Elance

302 Articles Published

Articles by Pradeep Elance

Page 18 of 31

Equate two list index elements in Python

Pradeep Elance
Pradeep Elance
Updated on 05-May-2020 618 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
Pradeep Elance
Updated on 05-May-2020 584 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
Pradeep Elance
Updated on 05-May-2020 553 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
Pradeep Elance
Updated on 05-May-2020 2K+ 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
Pradeep Elance
Updated on 04-May-2020 331 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

Dictionary with index as value in Python

Pradeep Elance
Pradeep Elance
Updated on 04-May-2020 891 Views

In this article we will learn how to create a dictionary from another frequently used python collection namely list. An index or key is not part a list content. But in dictionary we need to have a key or index attached to every element which is referred as value.Using enumerateThe enumerate function adds a counter as the key of the enumerate object. SO we apply it to a given list and using a for loop. That creates the required dictionary where the keys are generated by the enumerate function.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 'Wed', 11, 11] # Given ...

Read More

Dictionary creation using list contents in Python

Pradeep Elance
Pradeep Elance
Updated on 04-May-2020 231 Views

Changing the collection type from one type to another is a very frequent need in python. In this article we will see how we create a dictionary when multiple lists are given. The challenge is to be able to combine all these lists to create one dictionary accommodating all these values in a dictionary key value format.With zipThe zip function can be used to combine the values of different lists as shown below. In the below example we have taken three lists as input and combine them to form a single dictionary. One of the list supplies the keys for ...

Read More

Delete items from dictionary while iterating in Python

Pradeep Elance
Pradeep Elance
Updated on 04-May-2020 565 Views

A python dictionary is a collection which is unordered, changeable and indexed. They have keys and values and each item is referred using the key. In this article we will explore the ways to delete the items form a dictionary.Using del with keysIn this approach we capture the key values that are needed to be deleted. Once we apply the del function, the key value pairs for those keys get deleted.Example Live Demo# Given dictionary ADict = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4:'Thu', 5:'Fri'} # Get keys with value in 2, 3. to_del = [key for key in ADict ...

Read More

Delete elements with frequency atmost K in Python

Pradeep Elance
Pradeep Elance
Updated on 04-May-2020 435 Views

While manipulating data from lists we may come across scenario where we have selectively remove elements form the list based on their frequency. In this article we will explore how to remove all elements from a list whose frequency is less than equal to 2. You can also change the value 2 to any number in the programs.With countThe count methods keep the count of each element in the list. So we use it with a for loop and put a condition to only keep the elements whose count is greater than 2.Example Live DemolistA = ['Mon', 3, 'Tue', 'Mon', 9, ...

Read More

Delete elements in range in Python

Pradeep Elance
Pradeep Elance
Updated on 04-May-2020 874 Views

Deleting a single element from a python is straight forward by using the index of the element and the del function. But there may be scenarios when we need to delete elements for a group of indices. This article explores the approaches to delete only those elements form the list which are specified in the index list.Using sort and delIn this approach we create a list containing the index values where deletion has to happen. The we sort and reverse them to preserve the original order of the elements of the list. Finally we apply the del function to the ...

Read More
Showing 171–180 of 302 articles
« Prev 1 16 17 18 19 20 31 Next »
Advertisements