How to Add Elements to a Python Set

Problem Formulation and Solution Overview This article will show you how to add elements to a Python set. ℹ️ Info: A Python set is a collection of unique item(s) saved in no particular order (unordered). A set cannot be changed. However, elements can be added and removed. Sets can also perform calculations like union, intersection, … 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 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

How to Check if a Python Set is Empty?

There are three main ways to check if a set is empty: Implicit: Pass the set into a Boolean context without modification. Python will automatically convert it to a Boolean using bool(). Explicit: Pass the set into the bool() Function. Comparison: Compare the set with the empty set like so: my_set == set() Let’s dive … Read more

How to Fix TypeError: unhashable type: ‘list’

The TypeError: unhashable type: ‘list’ usually occurs when you try to use a list object as a set element or dictionary key and Python internally passes the unhashable list into the hash() function. But as lists are mutable objects, they do not have a fixed hash value. The easiest way to fix this error is … Read more