(Solved) Python TypeError: ‘float’ object is not subscriptable

Problem Formulation Consider the following minimal example where a TypeError: ‘float’ object is not subscriptable occurs: This yields the following output: Solution Overview Python raises the TypeError: ‘float’ object is not subscriptable if you use indexing or slicing with the square bracket notation on a float variable that is not indexable. However, the float class … Read more

How to Convert a Float List to a String List in Python

The most Pythonic way to convert a list of floats fs to a list of strings is to use the one-liner fs = [str(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to a string value using the str(x) constructor. This … Read more

[Fixed] Matplotlib: TypeError: ‘AxesSubplot’ object is not subscriptable

Problem Formulation Say, you’re me πŸ‘±β€β™‚οΈ five minutes ago, and you want to create a Matplotlib plot using the following (genius) code snippet: If you run this code, instead of the desired plot, you get the following TypeError: ‘AxesSubplot’ object is not subscriptable: πŸ’¬ Question: How to resolve the TypeError: ‘AxesSubplot’ object is not subscriptable … Read more

How to Print a List Without Newline in Python?

Best Solution to Print List Without Newline To print all values of a Python list without the trailing newline character or line break, pass the end=” argument in your print() function call. Python will then avoid adding a newline character after the printed output. For example, the expression print(*[1, 2, 3], end=”) will print without … Read more

Python Convert Parquet to CSV

Problem πŸ’¬ Challenge: How to convert a Parquet file ‘my_file.parquet’ to a CSV file ‘my_file.csv’ in Python? In case you don’t know what a Parquet file is, here’s the definition: πŸ’‘ Info: Apache Parquet is an open-source, column-oriented data file format designed for efficient data storage and retrieval using data compression and encoding schemes to … Read more

Python Programming Tutorial [+Cheat Sheets]

(Reading time: 19 minutes) The purpose of this article is to help you refresh your knowledge of all the basic Python keywords, data structures, and fundamentals. I wrote it for the intermediate Python programmer who wants to reach the next level of programming expertise. The way of achieving an expert level is through studying the … Read more

Python Convert Markdown Table to CSV

Problem Given the following Markdown table stored in ‘my_file.md’: 🐍 Python Challenge: How to convert the Markdown table to a CSV file ‘my_file.csv’? Solution To convert a Markdown table .md file to a CSV file in Python, first read the Markdown table file by using the f.readlines() method on the opened file object f, by … Read more

Double Hyphen (‐‐) Not Single Dash (–) in WordPress

WordPress automatically replaces ‐‐ with the long dash –. But for technical text, you often need the double minus symbol, e.g., when writing scripts and command line flags such as –option. πŸ’¬ How to prevent WordPress from replacing the double hyphen (minus) with the long dash? Here’s the surprisingly simple fix: You can simply edit … Read more

Plot Circle in Pyplot

How to Plot a Circle in Python? You can easily plot a circle in Matplotlib’s Pyplot library by calling plt.gca() to “get the current axis”. Then call the axis.add_patch() function and pass a plt.Circle() object into it. For example, the one-liner plt.gca().add_patch(plt.Circle((0.5, 0.5), 0.2, color=’lightblue’)) adds a lightblue circle to the plot at position x=0.5, … Read more