How to use range(len()) in Python?

Problem Formulation Have you come across the usage of range(len()) while trying to iterate across all the items of a given iterable? Now, this brings up a couple of questions – (i) Why do we use range(len())? (ii) How do we use range(len())? πŸ“Solution Generally, range(len()) allows you to iterate across a given iterable/sequence to … Read more

How to Reverse a Range in Python?

Summary: Use negative step in the range() function to reverse the given sequnce as: range(start, stop, -step). Another workaround is to apply the reversed() function to the given range to reverse it as: reversed(range(start, stop)). Problem: Given a range/sequence of numbers, how will you reverse the range? Example: Given:# Given rangefor i in range(1, 6): … Read more

Combine Images Using Numpy

Summary: You can combine images represented in the form of Numpy arrays using the concatenate function of the Numpy library as np.concatenate((numpydata_1, numpydata_2), axis=1). This combines the images horizontally. Use syntax: np.concatenate((numpydata_1, numpydata_2), axis=0) to combine the images vertically. Problem Formulation Consider you have two images represented as Numpy arrays of pixels. How will you … Read more

How to Remove Multiple Keys from a Python Dictionary

Summary:Β You can use one of these methods to delete multiple keys from a dictionary Python –(1) Iterate across the list of keys to be deleted and use the syntax del dict[β€˜key’](2) Iterate across the list of keys to be deleted and call the dictionary.pop(key)method.(3) Eliminate unrequired keys and reconstruct the original dictionary using a dictionary … Read more

How to Shuffle Two Arrays in Unison in Python?

Problem Formulation Given two arrays of the same length but different orders. How will you shuffle the two arrays in unison? Shuffling two arrays in unison means reordering the elements of both arrays in the same pattern. Let’s understand this with the help of an example. Example: Given:arr_1 = [[1, 1], [2, 2], [3, 3]]arr_2 … Read more

Python One Line Pattern

Problem Formulation You appear at an interview, and the interviewer intends to test your skills in writing concise one-line solutions. So he decides to present you with a long and complex-looking piece of code that is seemingly intimidating and asks you to come up with one-line solutions for the given code. Given: Challenges: Guess the … Read more