Found 10476 Articles for Python

Element repetition in list in Python

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

512 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

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
Updated on 04-May-2020 13:11:55

289 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
Updated on 04-May-2020 13:10:53

834 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 to list of tuple conversion in Python

Pradeep Elance
Updated on 04-May-2020 13:09:44

480 Views

ging 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 tuple from the key value pairs that are present in a dictionary. Each of the key value pair becomes a tuple. So the final list is a list whose elements are tuples.With items()We sue the items method of the dictionary which allows us to iterate through each of the key value pairs. Then we use a for loop to pack those values in to a tuple. We put all these tuples to a final ... Read More

Dictionary creation using list contents in Python

Pradeep Elance
Updated on 04-May-2020 13:08:06

175 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
Updated on 04-May-2020 13:06:48

496 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 in range in Python

Pradeep Elance
Updated on 04-May-2020 13:03:02

818 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

Decimal to binary list conversion in Python

Pradeep Elance
Updated on 04-May-2020 13:01:28

796 Views

Python being a versatile language can handle many requirements that comes up during the data processing. When we need to convert a decimal number to a binary number we can use the following python programs.Using formatWe can Use the letter in the formatter to indicate which number base: decimal, hex, octal, or binary we want our number to be formatted. In the below example we take the formatter as 0:0b then supply the integer to the format function which needs to get converted to binary.Example Live DemoDnum = 11 print("Given decimal : " + str(Dnum)) # Decimal to binary ... Read More

Custom list split in Python

Pradeep Elance
Updated on 04-May-2020 13:00:43

498 Views

Data analytics throws complex scenarios where the data need to be wrangled to moved around. In this context let’s see how we can take a big list and split it into many sublists as per the requirement. In this article we will explore the approaches to achieve this.With zip and for loopIn this approach we use list dicing to get the elements from the point at which the splitting has to happen. Then we use zip and for loop to create the sublists using a for loop.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] ... Read More

Advertisements