
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
Found 33676 Articles for Programming

515 Views
Data analysis can throw a variety of challenges. In this article we will take a list with numbers as its elements. Then we will find such pairs of elements in the list which has maximum difference in value between them.With nlargestThe approach here is to first find out all possible combinations of elements and then subtract the second element from the first. Finally apply the nlargest function form heapq module to get those pairs where the difference is maximum.Example Live Demofrom itertools import combinations from heapq import nlargest listA = [21, 14, 30, 11, 17, 18] # Given list ... Read More

398 Views
A given string may be a mixture of digits and letters. In this article we are required to find the biggest substring that has letters and digits together.with re moduleThe regular expression module can be used to find all the continuous substring having digits or letters. Then we apply the max function to pick only those continuous substrings of letters and digits which have maximum length among all the substrings found. The findall function is also use to identify and get the required substrings.Example Live Demoimport re def longSubstring(str): letter = max(re.findall(r'\D+', str), key=len) digit = max(re.findall(r'\d+', str), ... Read More

5K+ Views
While dealing with dictionaries, we may come across situation when there are duplicate values in the dictionary while obviously the keys remain unique. In this article we will see how we can achieve thi.Exchanging keys and valuesWe exchange the keys with values of the dictionaries and then keep appending the values associated with a given key. This way the duplicate values get clubbed and we can see them in the resulting new dictionary.Example Live DemodictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3} print("Given Dictionary :", dictA) k_v_exchanged = {} for key, value in dictA.items(): if ... Read More

214 Views
We have a scenario where we have to pick the top n longest word from a list containing many words of varying length. In this article we will see various approaches to achieve that.With count() and sorted()We first sort the elements of the list in the reverse order so that the longest words are available at the beginning of the list. Then find the length of each word and add the result of the count to a variable. Finally take a slice of the required number of longest words we need.Example Live Demofrom itertools import count def longwords(l, x): ... Read More

701 Views
Manytime when dealing with data analysis we may come across the None values present in a list. These values can not be used directly in mathematical operations and string operations etc. So we need to find their position and either convert them or use them effectively.With range()Combining the range and len function we can compare the value of each element with None and capture their index positions. Of course we use a for loop design to achieve this.Example Live DemolistA = ['Sun', 'Mon', None, 'Wed', None, None] # Given list print("Given list : ", listA) # Using range positions ... Read More

3K+ Views
A fibinacci series is a widley known mathematical series that explains many natural phenomenon. It starts with 0 and 1 and then goes on adding a term to its previous term to get the next term. In this article we will see how to generate a given number of elements of the Fibonacci series by using the lambda function in python.With sum and mapWe use the map function to apply the lambda function to each element of the list. We design a list slicing mechanism to get sum of previous two terms and use range to keep count of how ... Read More

2K+ Views
Sometime while processing data using the numpy library, we may need to filter out certain numbers in a specific range. This can be achieved by using some in-built methods available in numpy.With and operatorIn this approach we take an numpy array then apply the logical_and function to it. The where clause in numpy is also used to apply the and condition. The result is an array showing the position of the elements satisfying the required range conditions.import numpy as np A = np.array([5, 9, 11, 4, 31, 27, 8]) # printing initial array print("Given Array : ", A) ... Read More

897 Views
A python dictionary can be nested i.e., there are dictionaries within a dictionary. In this article we will see how to calculated the level of nesting in a dictionary when there is a nested dictionary.With string conversionIn this approach we convert the entire dictionary into a string. Then we count the number of left { that indicate to which level the dictionaries are nested.Example Live DemodictA = {1: 'Sun', 2: {3: {4:'Mon'}}} dictStr = str(dictA) cnt = 0 for i in dictStr : if i == "{": cnt += 1 print("The depth of dictionary: ", cnt)OutputRunning the ... Read More

317 Views
While manipulating data using python we may come across situation where we need to find the elements which are common among the multiple arrays. This can be achieved by converting the arrayd into dictionaries as shown below.In the below example we take the arrays and apply the Counter container from collections module. It will hold the count of each of the elements present in the container. Then we convert them to a dictionary by applying dict() and using the & operator to identify only the common elements among the arrays. Finally we loop through the items of the newly created ... Read More

1K+ Views
In a list of numbers we want to find out which three elements can join to give a certain sum. We call it a triplet. And in the list there can be many such triplets. For example, the sum 10 can be generated form numbers 1, 6, 3 as well as 1, 5, 4. In this article we will see how to find out all such triplets from a given list of numbers.Using range and temp variablesThis is the traditional approach in which we will create temporary variables. These variables will hold the elements from the list and check if ... Read More