Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1966 of 3366
847 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
551 Views
Suppose we have a zero-indexed array A of length N that contains all integers from 0 to N-1. We have to find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below. Now consider the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right before a duplicate element occurs in S. So if the array is like A = [5, 4, 0, 3, 1, 6, ... Read More
491 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
186 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
507 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
270 Views
Suppose we have a positive 32-bit integer n, we need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If we have no such positive 32-bit integer number, then return -1.So if the number is 213, then the result will be 231.To solve this, we will follow these steps −s := n as string, sz := size of s, ok := falsefor i in range sz – 2 to 0if s[i] < s[i + 1], then ok := true and break the loopif of is ... Read More
376 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
829 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
329 Views
Suppose we have a list of positive integers; the adjacent integers will perform the float division. So for example, [2, 3, 4] -> 2 / 3 / 4. Now, we can add any number of parenthesis at any position to change the priority of these operations. We should find out how to add parenthesis to get the maximum result, we have to find the corresponding expression in string format. Our expression should NOT contain redundant parenthesis. So if the input is like [1000, 100, 10, 2], then the result will be “1000/(100/10/2)”.To solve this, we will follow these steps −n ... Read More
812 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