Software engineering and personal development

Tag: value

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 Falsy Values from Lists in Python

One of the most attractive features of Python is its readability and conciseness, allowing developers to write elegant and efficient code. In this article, we will explore a Python method called “Compact” that helps remove falsy values from a list using filter().

The Compact method is a simple yet powerful Python function that removes falsy values (False, None, 0, and “”) from a list. It does this by utilizing the filter() function, which returns an iterator that includes only the elements of the list that satisfy a specific condition.

Continue reading

How to Get Default Values for Missing Keys in Python

Working with dictionaries in Python is a common task for many programmers, as it allows them to store and manipulate data in a key-value format. However, sometimes we may need to access a key in a dictionary that does not exist, and we want to provide a default value in such cases. The get() method in Python provides a simple and elegant solution to this problem.

The get() method is used to retrieve the value of a specified key in a dictionary. It takes two parameters: the key to look for and a default value to return if the key is not found in the dictionary. If the key is present in the dictionary, get() returns the corresponding value. Otherwise, it returns the default value specified.

Let’s look at the following code snippet:

dictionary = {'first_element': 1, 'second_element': 2}

print(dictionary.get('third_element', 3))  # 3

In this code, we have a dictionary with two key-value pairs. We then use the get() method to retrieve the value associated with the key 'third_element'. Since this key is not present in the dictionary, the method returns the default value of 3.

Continue reading

How to Quickly Avoid Errors when Getting Nonexisting Dictionary Elements

Dictionaries also known as maps are data structures that are used a lot in different scenarios. The process of getting an element from a dictionary can be done using an element that is not part of the dictionary which results in an error.

For example, let us take this scenario where we have a dictionary that has an element with the key name and another one with the element surname. If we want to access it using another element, such as age, we are going to see an error like the following:

 my_dictonary = {"name": "Name", "surname": "Surname"}
 print(my_dictonary["age"])  
Continue reading

How to Create a List with a Specific Length in Python

person writing bucket list on book
Photo by Glenn Carstens-Peters on Unsplash

When you create a new array in Java, you can set up the length of it. If you are programming mostly in Python these days, then you may not be aware that you can also do that in Python.

Let’s say that we have a fixed number of students and we want to save their heights in a list.

We can of course just declare an empty list and save their heights by appending new elements to the empty list. 

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑