If-Then-Else in One Line Python

Quick answer: How to put a simple if-then-else statement in one line of Python code? To put an if-then-else statement in one line, use Python’s ternary operator x if c else y. This returns the result of expression x if the Boolean condition c evaluates to True. Otherwise, the ternary operator returns the alternative expression … Read more

Python Find Longest List in List

Problem Formulation πŸ’¬ Programming Challenge: Given a list of lists (nested list). Find and return the longest inner list from the outer list of lists. Here are some examples: [[1], [2, 3], [4, 5, 6]] πŸ‘‰ [4, 5, 6] [[1, [2, 3], 4], [5, 6], [7]] πŸ‘‰ [1, [2, 3], 4] [[[1], [2], [3]], [4, … Read more

Python One Line Ternary

The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y. Ternary … Read more

How to Find the Shortest String in a Python Set?

Use Python’s built-in min() function with the key argument to find the shortest string in a set. Call min(my_set, key=len) to return the shortest string in the set using the built-in len() function to determine the weight of each stringβ€”the shortest string has minimum length. πŸ‘‰ Recommended Tutorial: How to Find the Shortest String in … Read more

How to Find the Shortest String in a Python Dictionary?

You’ll never guess the most concise way to solve this problem… πŸ¦„ Coding Challenge πŸ’¬ Challenge: Given a dictionary with string keys. Get the string key with the minimum number of characters, i.e., the shortest string. A variant of this problem is to get the length of the shortest string — I’ll give you a … Read more

Python – How to Find the Longest Substring in Alphabetical Order?

Programming Challenge πŸ’¬ Challenge: Given a Python string. Find the longest substring for which holds that all of its characters are in alphabetical order so that the first character comes earlier in the alphabet than the second character, and so on. Here are three examples: You get the point. πŸ™‚ Next, I’ll show you three … Read more

How to Check Python Version in Colab?

To check your Python version in Google’s Colab, type !python ‐‐version using the exclamation mark ! operator in your Jupyter Notebook cell and click on the “Run” icon. After a couple of seconds an output like Python 3.9 will appear, depending on the version you’ve installed. In Jupyter notebooks, the exclamation mark ! executes commands … Read more

How to Flatten a NumPy Array

Flatten to a 1D NumPy Array To flatten any NumPy array to a one-dimensional array, use the array.flatten() method that returns a new flattened 1D array. Here’s a simple example: Flatten NumPy Array Along Axis with reshape() To “flatten” a NumPy array along an axis, it’s often better to use the array.reshape() function. You can … Read more