How to Get a List Slice with Arbitrary Indices in Python?

To extract elements with specific indices from a Python list, use slicing list[start:stop:step]. If you cannot use slicing because there’s no pattern in the indices you want to access, use the list comprehension statement [lst[i] for i in indices], assuming your indices are stored in the variable indices. People always want to know the most … Read more

[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming

Q: What’s the object-oriented way to become wealthy? A: Inheritance. ? Your vocabulary determines the reality of your life. In this tutorial, I have compiled the most essential terms and concepts of object-oriented programming in Python. My goal was to create the best Python OOP cheat sheet that shows them in one place. Well — … Read more

How to Join Specific List Elements in Python?

To join specific list elements (e.g., with indices 0, 2, and 4) and return the joined string that’s the concatenation of all those, use the expression ”.join([lst[i] for i in [0, 2, 4]]). The list comprehension statement creates a list consisting of elements lst[0], lst[2], and lst[4]. The ”.join() method concatenates those elements using the … Read more

Introduction to Slicing in Python

Slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(lst), step=1). For example, the expression … Read more

Python | Join List as Path

Do you want to join a list of strings to a path in your specific operating system? Remembering the correct path separator can be a real pain. Fortunately, the os.path.join() method comes to the rescue! The os.path.join() method takes one or more path arguments and returns a concatenation of the path arguments with the correct … Read more

String Formatting: Keep It Simple, Stupid!

What is the output of this code snippet? The string format function is a useful feature that is new in Python 3. Use it to programmatically insert variable values into the string. Without the format function, you must break the string into a series of smaller substrings and concatenate them. For example, the last line … Read more

Python List Indexing

πŸ›‘ Note: Python has zero-based indexing, i.e., the index of the first element is 0, and the index of the i-th element is (i-1). Not considering zero-based indexing but assuming the i-th element has index i is a common source of bugs! Here’s an example that demonstrates how list indexing works in Python: πŸƒ Training … Read more

Set, Lambda, and Filter in Python

Social Media

What is the output of this code snippet? You will use or have already used the operations introduced in this puzzle. They are elementary pieces of knowledge for any Python programmer. First, we have the two dictionaries mapping an account name to the number of followers. For example, Cristiano Ronaldo (key: “@cristiano”) has 120 million … Read more

Parameter Passing in Python: Call By Object

What is the output of this code snippet? This puzzle demonstrates that parameters are called by object in Python. What does that mean? We look into two functions depreciation1 and depreciation2. The functions take an asset value or a list of asset values as inputs. They depreciate this value by multiplying it with the depreciation … Read more