
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 10476 Articles for Python

208 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

688 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

890 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

312 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

209 Views
we are given a string. The required task is to take out one letter from the string and print the remaining letters in the string. And this we have to so for each letter of the string.With loops and rangeThis is a basic programming approach in which we first list out the parameters required like declare the string, create variables for start and end positions and create a temporary placeholder for each of the letters. The we create a function that will iterate through each of the letters and create a string of remaining letters.Example Live Demolist = [] def ... Read More

354 Views
Many times we need to count the elements present in a list for some data processing. But there may be cases of nested lists and counting may not be straight forward. In this article we will see how to handle these complexities of counting number of elements in a list.With For loopIn this approach we use two for loops to go through the nesting structure of list. In the below program we have nested list where inner elements have different number of elements inside them. We also apply the len() function to calculate the length of the flattened list.Example Live DemolistA ... Read More

383 Views
In this article we are going to see how to count the numbers of pairs of numbers which have an exact difference equal to k. The given numbers are in form of a list and we supply the value of k to the program.Using for LoopIn this approach we design two for loops, one inside another. The outer for loop keeps track of visiting each element of the given list. The inner for loop keeps comparing each of the remaining elements with the element of the outer loop and increase the value of the count variable if it matches the ... Read More