Software engineering and personal development

Tag: set

How to Set Default Values for Missing Keys in Python Dictionaries

Python is renowned for its elegant and versatile syntax that often leads to efficient and concise code. In this quick blog post, we’re going to explore a handy Python trick that utilizes dictionaries to effortlessly assign default values for missing keys. This simple technique can save time and streamline your code, making it an essential tool for any Python developer.

Continue reading

How to Quickly Remove All Elements from a Data Structure in Python

Working with lists, sets, and dictionaries is a common task in Python programming. Often, we may need to remove all the elements from a list, set, or dictionary for various reasons. Python provides a simple and efficient way to clear all the elements from these data structures using the clear() method. In this article, we will discuss how to use the clear() method to remove all elements from a list, set, or dictionary.

Continue reading

How to Quickly Build Your Own Iterators in Python

You have probably had the chance to iterate through a list of elements in one way or another, or through elements of a set, or a dictionary. We can go through a list, a set, or a dictionary and access their elements because they are iterable objects.

An iterator is an object that contains a countable number of objects. This means that you can iterate through elements that an iterator contains.

Continue reading

How to Quickly Check Whether a List Contains Duplicate Elements in Python

white and yellow daisy in bloom
Photo by Stefan Cosma on Unsplash

Sometimes, you may need to check whether a list has any duplicate elements. This can be a common task when you are given a list of numbers or a list of strings. This can also be something that you may need to do both at work, or it can also be part of a coding challenge that you are implementing when being asked at a programming interview.

Continue reading

How to Quickly Find the Symmetric Difference of 2 Sets in Python

low-angle photography of brown concrete building during daytime
Photo by Samuel-Elias Nadler on Unsplash

If you have to find the union of elements that are either in the first or the second set, but not in both of them, then you need to use the symmetric difference.

This is also another math operation between sets that you have probably seen in a math class in high school.

This can be done quite easily and quickly using 2 built-in methods in Python.

Let’s start with the first one.

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑