Found 10784 Articles for Python

Python - Check if dictionary is empty

Pradeep Elance
Updated on 03-Mar-2020 06:17:58

5K+ Views

During analysis of data sets we may come across situations where we have to deal with empty dictionaries. In tis article we will see how to check if a dictionary is empty or not.Using ifThe if condition evaluates to true if the dictionary has elements. Otherwise it evaluates to false. So in the below program we will just check the emptiness of a dictionary using only the if condition.Example Live Demodict1 = {1:"Mon", 2:"Tue", 3:"Wed"} dict2 = {} # Given dictionaries print("The original dictionary : " ,(dict1)) print("The original dictionary : " ,(dict2)) # Check if dictionary is empty if dict1: ... Read More

Python - Check if all the values in a list are less than a given value

Pradeep Elance
Updated on 03-Mar-2020 06:12:29

885 Views

In python data analysis, we sometime face a situation where we need to compare a given number with a list containing many values. In this article we need to fins if a given number is less than each of the values present in a given list. We are going to achieve it using the following two ways.Using for loopWe iterate through the given list and compare the given value with each of the values in the list. Once all values from the list are compared and the comparison condition holds good in each of the step, we print out the ... Read More

Possible Words using given characters in Python

Pradeep Elance
Updated on 03-Mar-2020 06:11:04

649 Views

In this article we are going to see a python program that will give output of possible words from a given set of characters. Here we are taking a list as an input which will contain the set of reference words and another list containing the characters from which the words are made up of.In the below program, we define two functions. One to take the letters from the second list and make up words. Another function to match the words formed with the words present in the given list of words.Example Live Demodef Possible_Words(character):    x = {}    for ... Read More

Accessing all elements at given Python list of indexes

Pradeep Elance
Updated on 03-Mar-2020 05:31:23

256 Views

We can access the individual elements of a list using the [] brackets and the index number. But when we need to access some of the indices then we cannot apply this method. We need the below approaches to tackle this.Using two listsIn this method, along with the original list, we take the indices as another list. Then we use a for loop to iterate through the indices and supply those values to the main list for retrieving the values.Example Live Demogiven_list = ["Mon", "Tue", "Wed", "Thu", "Fri"] index_list = [1, 3, 4] # printing the lists print("Given list : ... Read More

Find K-Length Substrings With No Repeated Characters in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:31:57

425 Views

Suppose we have a string S, we have to find the number of substrings of length K where no characters are repeated. So if S = “heyfriendshowareyou” and K is 5, then output will be 15, as the strings are [heyfr, eyfri, yfrie, frien, riend, iends, endsh, ndsho, dshow, showa, howar, oware, warey, areyo, reyou]To solve this, we will follow these steps −create one empty map m, and left := 0 and right := -1 and ans := 0while right < length of string – 1if right – left + 1 = k, thenincrease ans by 1decrease m[str[left]] by 1increase ... Read More

Keys and Rooms in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:29:03

396 Views

Suppose we have N rooms and we start in room 0. In each room there exists a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. So in other words, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = number of rooms. A key rooms[i][j] = v, this opens the room with number v So if the input is [[1], [2], [3], []]. then output will be true. There are few more points that ... Read More

Maximum Swap in Python

Arnab Chakraborty
Updated on 29-Apr-2020 06:32:59

1K+ Views

Suppose we have a non-negative integer; we could swap two digits at most once to get the maximum valued number. We have to return the maximum valued number we can get. So if the input is like 2736 then the output will be 7236. So here we are swapping 2 and 7.To solve this, we will follow these steps −num := cut each digit from the number, and make a listnum1 := sort the num in reverse orderindex := 0while index < length of numif num1[index] is not same as num[index]a := subarray of num from index (index + 1) ... Read More

Palindromic Substrings in Python

Arnab Chakraborty
Updated on 29-Apr-2020 06:12:53

5K+ Views

Suppose we have a string; we have to count how many palindromic substrings present in this string. The substrings with different start indices or end indices are counted as different substrings even they consist of same characters. So if the input is like “aaa”, then the output will be 6 as there are six palindromic substrings like “a”, “a”, “a”, “aa”, “aa”, “aaa”To solve this, we will follow these steps −count := 0for i in range 0 to length if stringfor j in range i + 1 to length of string + 1temp := substring from index i to jif ... Read More

Minimum Moves to Equal Array Elements II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:41:57

366 Views

Suppose we have a non-empty integer array, we have to find the minimum number of moves that are required to make all array elements equal, where a move is incrementing or decrementing a selected element by 1. So when the array is like [1, 2, 3], then the output will be 2, as 1 will be incremented to 2, and 3 will be decremented to 2.To solve this, we will follow these steps −sort the array numsset counter as 0for i in nums, docounter := counter + absolute of (i – nums[length of nums / 2])return counterExample(Python)Let us see the ... Read More

4Sum II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:09:00

645 Views

Suppose we have four lists A, B, C, D of integer values, we have to compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. Consider all A, B, C, D have same length of N where 0 ≤ N ≤ 500. Remember all integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. So if the inputs are A = [1, 2], B = [-2, -1], C = [-1, 2], D = [0, 2], then ... Read More

Advertisements