Found 10784 Articles for Python

Dictionary with index as value in Python

Pradeep Elance
Updated on 04-May-2020 13:10:53

733 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

368 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

108 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

414 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

691 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

679 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

408 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

Create list of numbers with given range in Python

Pradeep Elance
Updated on 04-May-2020 12:59:09

15K+ Views

Python can handle any requirement in data manipulation through its wide variety of libraries and methods. When we need to generate all the numbers between a pair of given numbers, we can use python’s inbuilt functions as well as some of the libraries. This article describes such approaches.Using rangeThe range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 ending at a specified number. We can of curse change the starting, ending as well as increment steps to suit our need.Example Live Demodef getnums(s, e, i):    return list(range(s, e, i)) # Driver Code ... Read More

Compare Version Numbers in Python

Arnab Chakraborty
Updated on 02-May-2020 06:51:39

3K+ Views

Suppose we have to compare two version numbers version1 and version2. If the version1 > version2 then return 1; otherwise when version1 < version2 return -1; otherwise return 0. We can assume that the version strings are non-empty and contain only digits and the dot (.) characters. The dot character does not represent a decimal point and is used to separate number sequences. So for example, 2.5 is not "two and a half" or "halfway to version three", it is the fifth second-level revision of the second first-level revision.We can assume the default revision number for each level of a ... Read More

Longest Well-Performing Interval in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:40:28

177 Views

Suppose we have hours list, this is a list of the number of hours worked per day for a given employee. Here a day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. One well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. We have to find the length of the longest well-performing interval. So if the input is like [9, 9, 6, 0, 6, 6, 9], so then then the output will be ... Read More

Advertisements