How To Merge Two Python Dictionaries In A Single Expression In Python?

Summary: To merge two dictionaries dict1 and dict2 in a single expression, use the dictionary unpacking feature z = {**dict1, **dict2}. This creates a new dictionary and unpacks all (key-value) pairs into the new dictionary. Duplicate keys are automatically resolved by this method. Exercise: Which of the duplicated entry ends up in the dictionary? Other … Read more

How to Parse JSON in a Python One-Liner?

Problem: How to Parse a JSON object as a Python One-Liner? Example: Say, you have pulled a JSON object from a server using the curl command: Source How to parse the resulting JSON object and extract, say, the value “Netherlands” associated to the “country” key? Solution: The following one-liner accomplishes this: The command consists of … Read more

How to resolve “TypeError: can only concatenate str (not β€œint”) to str” in Python?

Many beginner programmers have a problem with the so-called TypeError, they wonder what they are doing wrong. Here’s an example: Exercise: Run the script and reproduce the error! Study this article to learn how to fix it! This article will answer every question that beginners may ask about this topic. About The TypeError Before showing … Read more

How to Create Inline Objects With Properties? [Python One-Liner]

Problem: How to create a Python object in a single line of code? And how can you associate custom properties to this object in the same line? Example: Say, you want to do something like this pseudocode snippet: However, you cannot do the same in Python because this would create a dictionary: This raises an … Read more

What is the Asterisk / Star Operator (*) in Python?

Asterisk Operator

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and … Read more

Python One Line And/Or

How do the Boolean and and or operators work in the context of Python one-liners? You may know the standard use of the logical operators applied to Boolean values: But there’s more to these operators that only experts in the art of writing concise Python one-liners know. For instance, the following use of the or … Read more

The Ultimate Guide To Python Tuples

Python has several built-in data structures such as Lists, Sets and Dictionaries (check out the articles!).In this article, you’ll learn everything you need to know about tuples including real world examples. A Python tuple is an immutable, ordered, and iterable container data structure that can hold arbitrary and heterogeneous immutable data elements. Tuple Motivating Example … Read more

[7 Steps] How to Start Your Freelancing Business on the Side?

Do you dream of creating your own coding business online as a web developer, Python freelancer, or programmer? Creating your coding business on the side is one of the best ways to test the waters and build crucial business skills before you fully commit to your new freelancing venture. Here are the eight steps to … Read more

Python One Line to Multiple Lines

To break one line into multiple lines in Python, use an opening parenthesis in the line you want to break. Now, Python expects the closing parenthesis in one of the next lines and the expression is evaluated across line boundaries. As an alternative, you can also use the backslash \ just in front of the … Read more