Python | Split String Key Value

Summary: There are three different ways to split the given string into keys and values: Minimal Example Problem Formulation Problem: Given a string containing key-value pairs. How will you split the string to get the key-value pairs? Example You have to split the string given in the problem below using the delimiter “:” in such … Read more

Python Create JSON File

Problem Formulation and Solution Overview This article focuses on working with a JSON file. JSON is an acronym for JavaScript Object Notation. This is a flat-text file formatted based on JavaScript (JS) Syntax. This file is most commonly noted for its ability to transmit data to/from web applications, such as sending/receiving data from a Server/Client … Read more

How to Count the Number of Unique Values in a List in Python?

Problem Statement:Β Consider that you have been given a list in Python. How will you count the number of unique values in the list? Example: Let’s visualize the problem with the help of an example: Given:Β li = [‘a’, ‘a’, ‘b’, ‘c’, ‘b’, ‘d’, ‘d’, ‘a’]Output:Β The unique values in the given list are ‘a’, ‘b’, ‘c’, ‘d’. … Read more

Python TypeError: ‘dict_values’ Not Subscriptable (Fix This Stupid Bug)

Do you encounter the following error message? TypeError: ‘dict_values’ object is not subscriptable You’re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started! Solution Python raises the “TypeError: ‘dict_values’ object is not subscriptable” if you … Read more

Python TypeError: ‘dict_keys’ Not Subscriptable (Fix This Stupid Bug)

Do you encounter the following error message? TypeError: ‘dict_keys’ object is not subscriptable You’re not alone! This short tutorial will show you why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started! Solution Python raises the “TypeError: ‘dict_keys’ object is not subscriptable” if you … Read more

Python Print Dictionary Keys Without “dict_keys”

Problem Formulation and Solution Overview If you print dictionary keys using print(dict.keys()), Python returns a dict_keys object, i.e., a view of the dictionary keys. The representation prints the keys enclosed in a weird dict_keys(…) wrapper text, e.g., dict_keys([1, 2, 3]). Here’s an example: There are multiple ways to change the string representation of the keys, … Read more

Python Print Dictionary Values Without “dict_values”

Problem Formulation and Solution Overview If you print all values from a dictionary in Python using print(dict.values()), Python returns a dict_values object, a view of the dictionary values. The representation prints the values enclosed in a weird dict_values(…) wrapper text, for example: dict_values([1, 2, 3]). Here’s an example: There are multiple ways to change the … Read more

Python Print Dictionary Without One or Multiple Key(s)

The most Pythonic way to print a dictionary except for one or multiple keys is to filter it using dictionary comprehension and pass the filtered dictionary into the print() function. There are multiple ways to accomplish this and I’ll show you the best ones in this tutorial. Let’s get started! πŸš€ πŸ‘‰ Recommended Tutorial: How … Read more