Summary: Use one of the following methods to format strings that contain curly braces:
Use double curly braces {{}}
Use the old string formatting, i.e. the % operator
Use the JSON Library
Use Template Strings
Problem: Given a string literal with curly braces; how to format the string and ensure that the curly braces are available in the output?
To understand the problem statement, let us have a look at the following example:
Example:
x = "{Serial No.}{0} ".format(1)
print(x)
Output:
Traceback (most recent call last): File "main.py", line 1, in <module> x = "{Serial No.}{0} ".format(1)
KeyError: 'Serial No'
From the above example it is clear that when we execute a print statement with a literal text that contains the curly braces, the program raises a KeyError unless we provide the proper syntax for string formatting. In this article, we shall be discussing the methods to overcome this problem and print our text with the curly braces along with string formatting. Therefore:
Desired Output:
{Serial No:}1
Before we dive into the solutions, let us have a look at the reason behind the KeyError exception.
KeyError Exception
A KeyError is raised when you try to access or look for a value in a dictionary that does not exist. For example, consider the following dictionary:
The above code will raise a KeyError exception. This is because we are trying to access the key ‘age‘ which does not exist within the dictionary profile. Since the key does not exist, we cannot access any value using this key.
Here’s what we get when we execute the above program:
Traceback (most recent call last): File "main.py", line 8, in <module> print(profile['age'])
KeyError: 'age'
No, the question that needs to be addressed is – “Why are we getting the KeyErorwhile formatting a string that contains a text along with curly braces?”
Reason: The .format() generally expects things inside { } to be keys but in this case, it is unable to do so since ‘Serial No.' is not a key. Therefore .format() unable to parse the data. This results in a KeyError as we are trying to access a key-value that does not exist.
Now that we know why we are getting the KeyError let us dive into the solutions to avoid this error.
Method 1: Using Double Curly Braces
We already discussed that {} inside a format string are special characters, therefore if we want to include braces as a part of our literal text, we need to tell the .format string parser that the given curly braces must be escaped and considered as a part of the given text literal. This can be easily done by doubling the curly braces encompassing the string, that is using the following syntax: {{Serial No.}}
The following program denotes how we can use double curly braces to reach our solution:
If you do not want to use the double curly braces then you might fancy the old style of string formatting that uses the modulo (%) operator. Let us have a look at the following program to understand how we can use the modulo operator to print our string along with curly braces in it.
x = " {Serial No.}%s"
print (x%(1))
Output
{Serial No.}1
Method 3: Using The JSON Library
In situations where you need to deal with complex JSON strings, the most efficient method of dealing with such scenarios is to use the JSON library in your program.
★ The json.dumps() method is used to covert a Python object, like a dictionary, to a JSON string.
Consider the following example which explains how we can use the JSON library to print JSON strings:
import json group = "Admin"
id = 1111
x = {"ID" : id, "Group" : group}
print(json.dumps(x))
Output:
{"ID": 1111, "Group": "Admin"}
Have a look at the following snippet given below to compare and contrast how complex and messy the syntax becomes when we try to print the same string using {{}} in our program.
group = "Admin"
id = 1111
print('{{"ID": {}, "Group": {}}}'.format(id,group))
Output:
{"ID": 1111, "Group": Admin}
Method 4: Using Template Strings
Template strings are used to provide string substitutions. If you want to avoid extra curly braces and % based substitutions then you can use the Template class of the string module.
★ The substitute() method performs template substitution a returns a new string.
Disclaimer: This might be a little confusing and prone to several exceptions if not properly used which is why I personally do not recommend you to use this procedure unless absolutely necessary.
Let us have a look at the following program to understand the usage of Template strings:
from string import Template
x = Template("$open Serial No: $close")
x = x.substitute(open='{',close='}')
print('{} {}'.format(x,1))
Output:
{ Serial No: } 1
EXERCISE
Now let’s get our hands dirty and practice some coding. Can you guess the output of the following snippet?
Note: Make sure you follow the comments in the given snippet which will unlock an important concept for you!
greet = "HELLO FINXTER"
name = input("Enter Your Name: ")
age = input("Enter Your Age:")
print("\n")
# to resolve an expression in the brackets instead of using literal text use three sets of curly braces
print(f"{{{greet.lower()}}} {name}")
print('{{You are {} years old!}}'.format(age))
Conclusion
In this article, we discussed various methods to format a string that contains curly braces in Python. Frankly speaking, the double curly braces is the simplest solution while using the JSON Library is the most efficient method while dealing with complex JSON data. However, you are always free to use any method that suits your requirements and the best way of getting hold of all these techniques is to practice them. So without further delay, please try them in your system, and please feel free to drop queries.
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
You’ll first learn about the definition of freelancing. Then, I’ll show you how you can evaluate whether the freelance developing niche is attractive for you and whether you can expect it to grow over time. So, let’s get started, shall we?
Definition
Freelancing is the act of delivering a service to another business or another customer in exchange for a defined rate.
If you travel back in time—say, ten years—freelancing would be the act of delivering your services to another business: a B2B (business-to-business) transaction.
But, since the appearance of freelancing platforms such as Upwork or Fiverr, it more and more became a B2C (business-to-customer) transaction. There are plenty of people, often employees, who need your services to become more and more productive.
In essence, you’re solving problems for other people. These people can be businesses, private persons, or employees. These people hire you to solve a problem for them. This makes perfect sense: in our world, everyone is a business owner.
As a person, employee, or freelancer, you are a one-person business that gets hired by organizations and other businesses.
As an employee, you are already a freelancer—have a look at the definition again. You sell your services to another party. You get paid by the hour. If you have experience as an employee, you have experience as a freelancer, too, because being an employee is nothing but a special case of being a freelancer.
But there are many more forms of freelancing. As an employee, you’re in a contract between your employer and yourself that ranges for many months. As a freelancer, you can also have these types of contracts: You can agree to contracts that range many years—in fact, businesses hire freelancers often on a long-term basis. If it makes economic sense to hire you once, why shouldn’t it make sense to hire you on a regular basis? But you can also have much smaller contracts that range only for a few hours.
Freelancing comes with all kinds of advantages and disadvantages. But as the term freelancing is so broadly defined, you cannot really generalize those: no advantage and no disadvantage will apply to any type of freelancing gig. Well, as a freelancer, you can aim for the best of both worlds: income security and higher income—if you design your freelancing business in an intelligent way.
Let’s have a deeper look into the freelance developer niche—is it attractive?
About the Freelance Developing Niche
Make no mistake: niche selection is critical.
Many people will tell you that you can select any niche. But this is only partially true.
Sure, if you join the top 10% of people in any niche, you’ll earn a lot of money and you’ll succeed in your profession.
But if you select the right niche, you can earn 10x or even 100x as a person in the top 10%. An example would be the niche “journalism” vs “machine learning engineer“.
As a top journalist, you can expect to earn $50,000-$100,000 per year. (source)
As a top machine learning engineer, you can expect to earn $200,000-$1,000,000 per year. (source)
That’s 4x to 10x difference in earnings of the top guys and gals! Niche selection is crucial.
Python Employee vs Freelancer
So, you may ask: should you go into the freelance developing niche—for instance, Python freelancing—or should you go into the pure Python development niche and become an employee?
I’ve recently read a book from the great Richard Koch: The Star Principle. He’s the author of The 80/20 Principle as well and he’s worth hundreds of millions of dollars. How has he done it?
He invests all his money in so-called “star companies”. And he has worked all his life in the same “star companies”. These companies generate lots of cash and everyone who’s involved benefits from their cash-generating ability.
A star business is an industry leader in a high-growth industry. This concept was developed by the Boston Consulting Group many decades ago—but it still applies to today’s businesses. Have a look at the matrix taken from BCG:
You want to invest your time and money only into businesses that are in high-growth markets and that have a high market share. An example is Google as the leader in the search engine market when the search engine market was still growing by more than 10% per year. Today, Google would be a “Cash Cow” according to the model—still attractive but not necessarily a star anymore.
The combination of being an industry leader and being in a high-growth market is very powerful.
As an industry leader, you have higher profit margins and more cash to reinvest than any other player in the market. This allows you to keep your growth rate over each other player in the market. Plus, you enjoy strong network effects (“the rich get richer”)—everyone knows you’re the leader so customers will come to you which reinforces your position as the leader.
As a company in a high-growth market, you will grow significantly even if you only maintain your market share.
If you can participate in a company that is the leader in a high-growth niche, you can expect significant benefits (if you don’t overpay as an investor).
So, how does it apply to the freelance developer niche?
The freelancing niche is growing double digits every year. Both companies Upwork and Fiverr (the industry leaders) grow more than 10% per year for many years.
These companies are out to disrupt the organization of the world’s talents. And if they keep growing, they’ll accomplish it!
As a developer, as a coder, you’re in an industry that grows 5% per year based on my estimation. It’s an attractive industry but it’s not a “star industry” anymore. Coding is still important and it will grow in importance over time. But it is not a high-growth niche anymore.
As a freelance developer though, you’re both in the freelancing and in the developer niche. Both grow significantly and their growth compounds. So, being a freelance developer is an extremely attractive niche.
If you combine it with Python which is the fastest-growing major programming language, you obtain a combination that has a high potential to transform your life.
If you want to participate in this disruptive trend, you should consider becoming a Python freelancer. Check out my Python freelancer course to get this going FAST!
Leaving the Rat Race
Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!
Do you want to work from home and earn a healthy living as a freelance developer? There never has been a better time! Freelance Developers make $51 per hour, on average, in the US.
Check those resources out—because success is about being prepared! All links open in a new tab.
Freelance Developer First Steps
Freelance Developer About
Freelance Developer Definition
Freelance Developer Skills
Freelance Developer Description
Freelance Developer Meaning
Freelance Developer Without Experience
Freelance Developer Work From Home
Freelance Developer Remote
Freelance Developer Experience
Freelance Developer Languages
Freelance Developer Niche
Freelance Developer Salary
Freelance Developer Hourly Rate
Freelance Developer Yearly Rate
Freelance Developer Income
Freelance Developer Estimates
Freelance Developer Price
Freelance Developer Pay
Freelance Developer Average Income
Freelance Developer Salary
Freelance Developer Hourly Rate (UK, Germany, Hong Kong, …)
Freelance Developer Net Worth
Freelance Developer Make
Freelance Developer Finding Jobs
Freelance Developer Gigs
Freelance Developer Projects
Freelance Developer Job Description
Freelance Developer Vacancy
Freelance Developer Jobs Online
Freelance Developer Platform
Freelance Developer Sites
Freelance Developer for Hire
Freelance Developer LinkedIn
Freelance Developer Marketplace
Freelance Developer Community
Freelance Developer Forum
Freelance Developer Education
Freelance Developer Udemy
Freelance Developer Guides and How-tos
Freelance Developer Course
Freelance Developer Masterclass
Freelance Developer Book
Freelance Developer Podcast
Freelance Developer Blog
Freelance Developer Group
Freelance Developer Help
Freelance Developer Quora
Freelance Developer Reddit
Freelance Developer Tips
Freelance Developer Tools & Templates
Freelance Developer Resume
Freelance Developer Website
Freelance Developer Website Examples
Freelance Developer Website Templates
Freelance Developer Employment Documents
Freelance Developer Templates
Freelance Developer Profile
Freelance Developer Portfolio
Freelance Developer Personal Website
Freelance Developer Tools
Freelance Developer Contract Template
Freelance Developer Invoice Template
Freelance Developer Agreement
Freelance Developer Logo
Freelance Developer Contract Template
Freelance Developer CV
Freelance Developer Contract
Freelance Developer Bio
Freelance Developer Branding
Freelance Developer Legal
Freelance Developer Taxes
Freelance Developer LLC
Freelance Developer Work Specializations
Freelance Web Developer Online
Freelance Qt Developer
Freelance Quant Developer
Freelance Software Developer
Freelance Java Developer
Freelance Developer Embedded
Freelance eLearning Developer
Freelance Email Developer
Freelance Email Developer Jobs
Freelance Developer Web
Freelance eCommerce Developer
Freelance ETL Developer
Freelance React Developer
Freelance Developer WordPress
Freelance Tableau Developer
Freelance Tally Developer
Freelance Zoho Developer
Freelance Unity Developer
Freelance UI Developer
Freelance Unreal Developer
Freelance Umbraco Developer
Freelance Frontend Developer
Freelance Flutter Developer
Freelance Full Stack Developer
Freelance Filemaker Developer
Freelance Firmware Developer
Freelance Flask Developer
Freelance Frontend Developer
Freelance Game Developer
Freelance Go Developer
Freelance GIS Developer
Freelance Hubspot Developer
Freelance HTML Developer
Freelance Labview Developer
Freelance VR Developer
Freelance VBA Developer
Freelance UI Developer
Freelance Video Game Developer
Freelance Backend Developer
Freelance Blockchain Developer
Freelance .Net Developer
Freelance Mobile Developer
Freelance Magento Developer
Freelance iOS Developer
Freelance IONIC Developer
Freelance Odoo Developer
Freelance Outsystems Developer
Freelance App Developer
Freelance Android Developer
Freelance Angular Developer
Freelance Database Developer
Freelance Django Developer
Freelance Drupal Developer
Freelance Delphi Developer
Freelance Oracle Developer
Hiring Freelance Developers
Freelance Developer Wanted / Required
Freelance Developer Agency
Freelance Developer Region Specifics
Freelance Developer US
Freelance Developer Germany
Freelance Developer UK
Freelance Developer India
Freelance Developer Canada
Freelance Developer Hong Kong
Freelance Developer Philippines
Freelance Developer Australia
Freelance Developer Indonesia
Freelance Developer Ukraine
Freelance Developer Malaysia
Freelance Developer Singapore
Freelance Developer South Africa
Freelance Developer Switzerland
Freelance Developer Dubai
Freelance Developer Berlin
Freelance Developer Gurgaon
Freelance Developer Kolkata
Freelance Developer London
Freelance Developer Leeds
Freelance Developer New York
Freelance Developer Toronto
Freelance Developer Chennai
Freelance Developer Vancouver
Freelance Developer Bangalore
Freelance Developer NZ
Freelance Developer Near Me
Freelance Developer New Zealand
Freelance Developer Newcastle
Freelance Developer Wellington
Freelance Developer in Cape Town
Freelance Developer Melbourne
Freelance Developer Manchester
Freelance Developer Midlands
Freelance Developer Oslo
Freelance Developer Amsterdam
Freelance Developer Sydney
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells.
But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Let’s get started!
Python One Line If Else
You can use a simple if statement in a single line of code. This is called the ternary operator. The most basic ternary operatorx if c else y returns expression x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative expression y.
Here’s a minimal example:
var = 21 if 3<2 else 42
print(var)
# 42
Ternary (from Latin ternarius) is an adjective meaning “composed of three items”. (source) So, literally, the ternary operator in Python is composed of three operands.
Syntax: The three operands are written as x if c else y which reads as “return x if c else return y“. Let’s write this more intuitively as:
<OnTrue> if <Condition> else <OnFalse>
Operand
Description
<OnTrue>
The return expression of the operator in case the condition evaluates to True
<Condition>
The condition that determines whether to return the <On True> or the <On False> branch.
<OnFalse>
The return expression of the operator in case the condition evaluates to False
By now, you’ve learned how to write the if-else statement in a single line of code using the ternary operator. But can you do the same with an elif statement if you have multiple conditions?
Of course, you can! (If you’re in doubt about whether you can do XYZ in a single line of Python, just assume that you can. Check out my new book “Python One-Liners” to master the single line of code!)
Say, you want to write the following if-then-else condition in a single line of code:
>>> x = 42
>>> if x > 42:
>>> print("no")
>>> elif x == 42:
>>> print("yes")
>>> else:
>>> print("maybe")
yes
The elif branch wins: you print the output "yes" to the shell. But how to do it in a single line of code? Just use the ternary operator with an elif statement won’t work (it’ll throw a syntax error).
The answer is simple: nest two ternary operators like so:
print("no") if x > 42 else print("yes") if x == 42 else print("maybe")
# yes
If the value x is larger than 42, we print “no” to the shell. Otherwise, we execute the remainder of the code (which is a ternary operator by itself). If the value x is equal to 42, we print “yes”, otherwise “maybe”.
So by nesting multiple ternary operators, we can greatly increase our Python one-liner power!
Now, you know how you can add more conditions to a single-line conditional statement. An interesting question is whether you can also add fewer conditions?
Python One Line If Without Else
Problem: What’s the one-liner equivalent of the simple if statement without an else branch?
Here’s an example:
condition = True if condition: print('hi') # hi
You may want to (i) print something, (ii) assign a value to a variable, or (iii) append an element to a list if the condition holds.
Next, I’ll show you four methods of how to accomplish this goal. All four methods are generally applicable—and you can easily customize them to your specific application.
Let’s have a quick overview of the four methods:
condition = True # Simple 2-Liner Method
if condition: print('hi') # Method 1: One-Liner If
if condition: print('hi') # Method 2: Ternary with Dummy
print('hi') if condition else None # Method 3: Ternary with Dummy for Assignment
x = 42 if condition else None # Method 4: Short circuiting
condition and print('hi')
The most Pythonic way to define a function in a single line is to (1) create an anonymous lambda function and (2) assign the function object to a variable name. You can then call the function by name just like any other regularly-defined function. For example, the statement f = lambda x: x+1 creates a function f that increments the argument x by one and returns the result: f(2) returns 3.
Problem: How to define a function in a single line of Python code? Let’s explore this mission-critical question!
Example: Say, you want to write the following function in a single line of code:
Python is powerful — you can condense many algorithms into a single line of Python code. So the natural question arises: can you write a for loop in a single line of code? This article explores this mission-critical question in all detail.
How to Write a For Loop in a Single Line of Python Code?
There are two ways of writing a one-liner for loop:
If the loop body consists of one statement, simply write this statement into the same line: for i in range(10): print(i). This prints the first 10 numbers to the shell (from 0 to 9).
If the purpose of the loop is to create a list, use list comprehension instead: squares = [i**2 for i in range(10)]. The code squares the first ten numbers and stores them in the list squares.
You can also modify the list comprehension statement by restricting the context with another if statement:
Problem: Say, we want to create a list of squared numbers—but you only consider even and ignore odd numbers.
Example: The multi-liner way would be the following.
squares = [] for i in range(10): if i%2==0: squares.append(i**2) print(squares)
# [0, 4, 16, 36, 64]
You create an empty list squares and successively add another square number starting from 0**2 and ending in 8**2—but only considering the even numbers 0, 2, 4, 6, 8. Thus, the result is the list [0, 4, 16, 36, 64].
Solution: Again, you can use list comprehension [i**2 for i in range(10) if i%2==0] with a restrictive if clause (in bold) in the context part to compress this in a single line of Python code:
print([i**2 for i in range(10) if i%2==0])
# [0, 4, 16, 36, 64]
This line accomplishes the same output with much less bits.
Problem: Given a collection. You want to create a new list based on all values in this collection. The code should run in a single line of code. How do you accomplish this? Do you need a lambda function?
Example: Given an array a = [1, 2, 3, 4]. You need to create a second array b with all values of a—while adding +1 to each value. Here’s your multi-liner:
a = [1, 2, 3, 4]
b = []
for x in a: b.append(x+1)
print(b)
# [2, 3, 4, 5]
How do you accomplish this in a single line of code?
Answer: No, you don’t need a lambda function. What you’re looking for is a feature called list comprehension. Here’s the one-liner expression that accomplishes this without the lambda function:
b = [x+1 for x in a]
print(b)
# [2, 3, 4, 5]
Let’s dive into some background information in case you wonder how list comprehensions work. Based on your question, I also suspect that you don’t completely understand lambda functions either, so I’ll also add another section about lambda functions. Finally, you’ll also learn about a third alternative method to solve this exact problem by using the lambda function in combination with Python’s built-in map() function!
So, stay with me—you’ll become a better coder in the process!
There are three ways of writing a one-liner while loop:
Method 1: If the loop body consists of one statement, write this statement into the same line: while True: print('hi'). This prints the string 'hi' to the shell for as long as you don’t interfere or your operating system forcefully terminates the execution.
Method 2: If the loop body consists of multiple statements, use the semicolon to separate them: while True: print('hi'), print('bye'). This runs the statements one after the other within the while loop.
Method 3: If the loop body consists nested compound statements, replace the inner compound structures with the ternary operator: while True: print('hi') if condition else print('bye').
# Method 1: Single-Statement Body
while True: print('hi') # Method 2: Multiple-Statements Body
c = 0
while c < 10: print(c); c = c + 1 # Method 3: Nested Statements Body
while True: print('yes') if True else print('no')
Want to create your own webserver in a single line of Python code? No problem, just use this command in your shell:
$ python -m http.server 8000
The terminal will tell you:
Serving HTTP on 0.0.0.0 port 8000
To shut down your webserver, kill the Python program with CTRL+c.
This works if you’ve Python 3 installed on your system. To check your version, use the command python --version in your shell.
You can run this command in your Windows Powershell, Win Command Line, MacOS Terminal, or Linux Bash Script.
You can see in the screenshot that the server runs on your local host listening on port 8000 (the standard HTTP port to serve web requests).
Note: The IP address is NOT 0.0.0.0—this is an often-confused mistake by many readers. Instead, your webserver listens at your “local” IP address 127.0.0.1 on port 8000. Thus, only web requests issued on your computer will arrive at this port. The webserver is NOT visible to the outside world.
Python 2: To run the same simple webserver on Python 2, you need to use another command using SimpleHTTPServerinstead of http:
$ python -m SimpleHTTPServer 8000
Serving HTTP on 0.0.0.0 port 8000 ...
If you want to start your webserver from within your Python script, no problem:
import http.server
import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("serving at port", PORT) httpd.serve_forever()
This code comes from the official Python documentation—feel free to read more if you’re interested in setting up the server (most of the code is relatively self-explanatory).
Problem: Given a string and a filename. How to write the string into the file with filename using only a single line of Python code?
Example: You have filename 'hello.txt' and you want to write string 'hello world!' into the file.
hi = 'hello world!'
file = 'hello.txt' # Write hi in file '''
# File: 'hello.txt':
hello world! '''
How to achieve this? Here are four ways of doing it in a single line of code!
Solution: Here’s a quick overview of the methods to accomplish this.
hi = 'hello world!'
file = 'hello.txt' # Method 1: 'with' statement
with open(file, 'a') as f: f.write(hi) # Method 2: print() function
print(hi, file=open(file, 'a')) # Method 3: multi-line statement
f = open(file, 'a'); f.write(hi); f.close() # Method 4: open() and write()
open(file, 'a').write(hi)
Exercise: Run the code and check the file 'hello.txt'. How many 'hello worlds!' are there in the file? Change the code so that only one 'hello world!' is in the file!
Most computer scientists, programmers, and hackers don’t even know the meaning of the word “Quine” in the context of programming. So, first things first:
Roughly speaking, a quine is a self-reproducing program: if you run it, it generates itself.
Here’s a great definition:
:quine: /kwi:n/ /n./ [from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (source)
The name “quine” was coined by Douglas Hofstadter, in his popular science book Gödel, Escher, Bach, in honor of philosopher Willard Van Orman Quine (1908–2000), who made an extensive study of indirect self-reference, and in particular for the following paradox-producing expression, known as Quine’s paradox.
In this one-liner tutorial, you’ll learn about the popular sorting algorithm Quicksort. Surprisingly, a single line of Python code is all you need to write the Quicksort algorithm!
Problem: Given a list of numerical values (integer or float). Sort the list in a single line of Python code using the popular Quicksort algorithm!
Example: You have list [4, 2, 1, 42, 3]. You want to sort the list in ascending order to obtain the new list [1, 2, 3, 4, 42].
Short answer: The following one-liner solution sorts the list recursively using the Quicksort algorithm:
q = lambda l: q([x for x in l[1:] if x <= l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else []
Now, let’s dive into some details!
The following introduction is based on my new book “Python One-Liners”(Amazon Link) that teaches you the power of the single line of code (use it wisely)!
Introduction: Quicksort is not only a popular question in many code interviews – asked by Google, Facebook, and Amazon – but also a practical sorting algorithm that is fast, concise, and readable. Because of its beauty, you won’t find many introduction to algorithm classes which don’t discuss the Quicksort algorithm.
Overview: Quicksort sorts a list by recursively dividing the big problem (sorting the list) into smaller problems (sorting two smaller lists) and combining the solutions from the smaller problems in a way that it solves the big problem. In order to solve each smaller problem, the same strategy is used recursively: the smaller problems are divided into even smaller subproblems, solved separately, and combined. Because of this strategy, Quicksort belongs to the class of “Divide and Conquer” algorithms.
Algorithm: The main idea of Quicksort is to select a pivot element and then placing all elements that are larger or equal than the pivot element to the right and all elements that are smaller than the pivot element to the left. Now, you have divided the big problem of sorting the list into two smaller subproblems: sorting the right and the left partition of the list. What you do now is to repeat this procedure recursively until you obtain a list with zero elements. This list is already sorted, so the recursion terminates.
The following Figure shows the Quicksort algorithm in action:
Figure: The Quicksort algorithm selects a pivot element, splits up the list into (i) an unsorted sublist with all elements that are smaller or equal than the pivot, and (ii) an unsorted sublist with all elements that are larger than the pivot. Next, the Quicksort algorithm is called recursively on the two unsorted sublists to sort them. As soon as the sublists contain maximally one element, they are sorted by definition – the recursion ends. At every recursion level, the three sublists (left, pivot, right) are concatenated before the resulting list is handed to the higher recursion level.
You create a function q which implements the Quicksort algorithm in a single line of Python code – and thus sorts any argument given as a list of integers.
## The Data
unsorted = [33, 2, 3, 45, 6, 54, 33] ## The One-Liner
q = lambda l: q([x for x in l[1:] if x <= l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else [] ## The Result
print(q(unsorted))
What is the output of this code?
## The Result
print(q(unsorted))
# [2, 3, 6, 33, 33, 45, 54]
Python One Line With Statement
Problem: Can you write the with statement in a single line of code?
Solution: Yes, you can write the with statement in a single line of code if the loop body consists only of one statement:
with open('code.py') as code: print(code.read())
In general, you can write any indentation block (like if statements, with environments, or while loops) in a single line of code if the body consists of only one statement.
Summary: You can accomplish one line exception handling with the exec() workaround by passing the one-linerized try/except block as a string into the function like this: exec('try:print(x)nexcept:print("Exception!")'). This general method works for all custom, even multi-line, try and except blocks. However, you should avoid this one-liner code due to the bad readability.
Surprisingly, there has been a discussion about one-line exception handling on the official Python mailing list in 2013. However, since then, there has been no new “One-Line Exception Handling” feature in Python. So, we need to stick with the methods shown in this tutorial. But they will be fun—promised!
Let’s dive into the problem:
Problem: How to write the try/except block in a single line of Python code?
Example: Consider the following try/except block.
try: print(x)
except: print('Exception!')
Solution: let’s have a quick overview in our interactive code shell:
# Original
try: print(x)
except: print('Exception!') # Method 1
print(x) if 'x' in dir() else print('Exception!') # Method 2
exec('try:print(x)\nexcept:print("Exception!")') # Method 3
from contextlib import suppress
with suppress(NameError): print(x)
Problem: Given a multi-line code script in Python. How to execute this multi-line script in a single line of Python code? How to do it from the command line?
Example: Say, you have the following for loop with a nested if statement in the for loop body. You want to run this in a single line from your command line?
x = 10
for i in range(5): if x%2 == 0: print(i) else: print(x) x = x - 1 '''
0
9
2
7
4 '''
The code prints five numbers to the shell. It only prints the odd values of x. If x takes an even value, it prints the loop variable i.
You can write any source code into a string and run the string using the built-in exec() function in Python. This is little known—yet, hackers often use this to pack malicious code into a single line that’s seemingly harmless.
If you have code that spans multiple lines, you can pack it into a single-line string by using the newline character 'n' in your string:
# Method 1
exec('x = 10nfor i in range(5):n if x%2 ==0: print(i)n else: print(x)n x = x-1')
This one-liner code snippet is semantically equivalent to the above nested for loop that requires seven lines of code! The output is the same:
A reverse shell is used by hackers to gain access to a target machine. The target machine opens a shell to communicate to the attacking machine. The attacking machine receives the connection (listening on a given port) and is now able to access the target computer. To accomplish a reverse shell, a hacker must execute code on a target machine. Reverse shells are also used by security engineers to test and prevent reverse shell attacks.
I found this code in a blog thread. You can run it from any computer with Python installed and visible from your current location:
Say your file is stored in file 'code.py'. Now, you can open the file, read all lines, get rid of leading and trailing whitespace characters, and store the result in a Python list in a single line of code. Here’s the code:
Problem: How to return from a Python function or method in single line?
Example: Consider the following “goal” statement:
def f(x): return None if x == 0
However, this leads to a Syntax error:
Here’s how to write the return statement with an if expression in a single line of Python code:
def f(x): if x==0: return None
I should note that PEP 8 is actually fine with writing if block statements into a single line. Nevertheless, the default return value of a function is None so the code does really nothing.
Two ways of writing a recursive one-liner: (1) write the function with return statement in a single line such as in def f(x): return f(x+1), or (2) assign a lambda function to a variable name and use the variable name in the return expression of the lambda function such as in f = lambda x: f(x). To define a recursion base case, you can use the ternary operator x if c else y to return x if condition c is met, else y.
Let’s dive into the problem and several detailed examples!
Problem: How to write a recursive function in a single line of code?
You may find this challenging because you need to define the function name, the base case, and the recursive function call—all in a single line of Python code!
Summary: To match a pattern in a given text using only a single line of Python code, use the one-liner import re; print(re.findall(pattern, text)) that imports the regular expression library re and prints the result of the findall() function to the shell.
The re.findall(pattern, string, flags=0) method returns a list of string matches. Read more in our blog tutorial.
There’s no better way of importing the re library and calling the re.findall() function in a single line of code—you must use the semicolon A;B to separate the statements A and B.
The findall() function finds all occurrences of the pattern in the string.
The replace method replaces all occurrences of the first argument with the second argument. It returns the new string. You can now print the result to the stdin or write it back to a file.
Ternary (from Latin ternarius) is an adjective meaning “composed of three items”. (source) So, literally, the ternary operator in Python is composed of three operands.
Syntax: The three operands are written in an intuitive combination ... if ... else ....
<On True> if <Condition> else <On False>
Operand
Description
<On True>
The return expression of the operator in case the condition evaluates to True
<Condition>
The condition that determines whether to return the <On True> or the <On False> branch.
<On False>
The return expression of the operator in case the condition evaluates to False
Operands of the Ternary Operator
Let’s have a look at a minimum example in our interactive code shell:
Exercise: Run the code and input your age. What’s the output? Run the code again and try to change the output!
Python One Line Two For Loops (Double)
Problem: How to write a nested for loop as a Python one-liner? Roughly speaking, you want to iterate over two or more iterables that are nested into each other. Here’s an example of a multi-liner with two nested loops:
iter1 = [1, 2, 3, 4]
iter2 = ['a', 'b', 'c'] for x in iter1: for y in iter2: print(x, y) '''
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c
4 a
4 b
4 c '''
Problem: Given multiple Python statements. How to write them as a Python One-Liner?
Example: Consider the following example of four statements in a block with uniform indentation:
a = 1
b = 2
c = a + b
print(c)
Each of the four statements is written in a separate line in a code editor—this is the normal procedure. However, what if you want to one-linerize those:
How to write all four statements in a single line of code?
Solution: The answer is simple if all statements have a uniform indentation and there’s no nested block. In this case, you can use the semicolon as a separator between the statements:
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 line break to escape the newline character.
URL encoding “is a method to encode information in a Uniform Resource Identifier (URI)”. It is also called Percent-encoding because percentage symbols are used to encode certain reserved characters:
!
#
$
%
&
'
(
)
*
+
,
/
:
;
=
?
@
[
]
%21
%23
%24
%25
%26
%27
%28
%29
%2A
%2B
%2C
%2F
%3A
%3B
%3D
%3F
%40
%5B
%5D
$ alias urldecode='python3 -c "import sys, urllib.parse as ul; print(ul.unquote_plus(sys.argv[1]))"' $ alias urlencode='python3 -c "import sys, urllib.parse as ul; print (ul.quote_plus(sys.argv[1]))"'
However, you cannot do the same in Python because this would create a dictionary:
new_object = {'property': 'value'}
new_object.property
# raises an AttributeError
This raises an AttributeError because there’s no attribute property associated to the dictionary object. To access the dictionary key in this case, you must use the syntax new_object['property'].
So, how to create an inline Python object with attributes in a single line of Python code?
Here’s a quick overview of our three methods:
Exercise: Do all three methods produce the same output? Run the code to test it!
Do you want to dive into each of those methods in greater detail? Read this article:
How can you filter an array in Python using an arbitrary condition?
The most Pythonic way of filtering an array is the list comprehension statement [x for x in list if condition]. You can replace condition with any function of x you would like to use as a filtering criterion.
For example, if you want to filter all elements that are smaller than, say, 10, you’d use the list comprehension statement [x for x in list if x<10] to create a new list with all list elements that are smaller than 10.
Here are three examples of filtering a list:
Get elements smaller than eight: [x for x in lst if x<8].
Get even elements: [x for x in lst if x%2==0].
Get odd elements: [x for x in lst if x%2].
lst = [8, 2, 6, 4, 3, 1] # Filter all elements <8
small = [x for x in lst if x<8]
print(small) # Filter all even elements
even = [x for x in lst if x%2==0]
print(even) # Filter all odd elements
odd = [x for x in lst if x%2]
print(odd)
The output is:
# Elements <8
[2, 6, 4, 3, 1] # Even Elements
[8, 2, 6, 4] # Odd Elements
[3, 1]
This is the most efficient way of filtering an array and it’s also the most Pythonic one.
Problem: How can we append multiple elements to a list in a for loop but using only a single line of Python code?
Example: Say, you want to filter a list of words against another list and store the resulting words in a new list using the append() method in a for loop.
# FINXTER TUTORIAL:
# How to filter a list of words? words = ['hi', 'hello', 'Python', 'a', 'the']
stop_words = {'a', 'the'}
filtered_words = [] for word in words: if word not in stop_words: filtered_words.append(word) print(filtered_words)
# ['hi', 'hello', 'Python']
You first create a list of words to be filtered and stored in an initially empty list filtered_words. Second, you create a set of stop words against you want to check the words in the list. Note that it’s far more efficient to use the set data structure for this because checking membership in sets is much faster than checking membership in lists. See this tutorial for a full guide on Python sets.
You now iterate over all elements in the list words and add them to the filtered_words list if they are not in the set stop_words.
Solution: You can one-linerize this filtering process using the following code:
filtered_words = [word for word in words if word not in stop_words]
Here’s the complete code that solves the problem using the one-liner filtering method:
# FINXTER TUTORIAL:
# How to filter a list of words? words = ['hi', 'hello', 'Python', 'a', 'the']
stop_words = {'a', 'the'}
filtered_words = [word for word in words if word not in stop_words] print(filtered_words)
# ['hi', 'hello', 'Python']
Here’s a short tutorial on filtering in case you need more explanations:
You may know the standard use of the logical operators applied to Boolean values:
>>> True and False
False
>>> False or True
True
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 operator applied to non-Boolean values is little known:
>>> 'hello' or 42 'hello'
>>> [] or 42
42
Similarly, the following use of the and operator often causes confusion in readers of advanced Python one-liners:
>>> 'hello' and 42
42
>>> [] and 42
[]
How do the and and or operator work when applied to non-Boolean operands?
To understand what is going on, you need to look at the definitions of the Boolean operators:
Operator
Description
a or b
Returns b if the expression a evaluates to False using implicit Boolean conversion. If the expression a evaluates to True, the expression a is returned.
a and b
Returns b if the expression a evaluates to True using implicit Boolean conversion. If the expression a evaluates to False, the expression a is returned.
Study these explanations thoroughly! The return value is of the same data type of the operands—they only return a Boolean value if the operands are already Boolean!
This optimization is called short-circuiting and it’s common practice in many programming languages. For example, it’s not necessary to evaluate the result of the second operand of an and operation if the first operand evaluates to False. The whole operation must evaluate to False in this case because the logical and only returns True if both operands are True.
Python goes one step further using the property of implicit Boolean conversion. Every object can be implicitly converted to a Boolean value. That’s why you see code like this:
l = []
if l: print('hi')
else: print('bye')
# bye
You pass a list into the if condition. Python then converts the list to a Boolean value to determine which branch to visit next. The empty list evaluates to False. All other lists evaluate to True, so the result is bye.
Together, short circuiting and implicit Boolean conversion allow the logical operators and and or to be applied to any two Python objects as operands. The return value always is one of the two operands using the short circuiting rules described in the table.
Problem: How to sum over all values in a given Python list?
a = [1, 2, 3]
You want to calculate the sum of all values in the list—using only a single line of Python code!
# RESULT: 6
Solution: Python’s built-in sum() function helps you to sum over all values in an iterable, such as a Python list. Here’s the minimal code example.
a = [1, 2, 3] print(sum(a))
# 6
How does it work? The syntax is sum(iterable, start=0):
Argument
Description
iterable
Sum over all elements in the iterable. This can be a list, a tuple, a set, or any other data structure that allows you to iterate over the elements. Example: sum([1, 2, 3]) returns 1+2+3=6.
start
(Optional.) The default start value is 0. If you define another start value, the sum of all values in the iterable will be added to this start value. Example: sum([1, 2, 3], 9) returns 9+1+2+3=15.
Please find more details in the complete tutorial about summing over all values in a list and a nested list:
To sort and return a Python list in a single line of code, use the sorted(list) method that returns a new list of sorted elements. It copies only the references to the original elements so the returned list is not a deep but a shallow copy.
You want to sort this list and return the result in a single line. If you use the list.sort() method, the return value is None:
print(a.sort())
# None
The return value of the list.sort() method is None, but many coders expect it to be the sorted list. So they’re surprised finding out that their variables contain the None type rather than a sorted list.
Here’s a quick overview of the methods to accomplish this:
Exercise: Change the list to be sorted by adding negative floats. Does it still work?
Do you want to learn more about each of the methods? Check out my related tutorial:
x = 'hi'; y = 'young'; z = 'friend'; print(x, y, z);
If you run this Python one-liner with semicolons, you’ll get the following output:
hi young friend
On the first view, it doesn’t even look like Python code! C++ has semicolons. Java has semicolons. But Python is supposed to be a semicolon-free language, isn’t it?
The meaning of the semicolon in programming languages such as Java and C++ is to terminate the current statement. In those languages, you’ll use it after every single line. Without it, the interpreter believes that the code has not terminated yet and it starts looking for more. Any Java or C++ coder knows situations where an error occurred because they forgot to use a semicolon in their code.
In Python, however, semicolons have a slightly different meaning. They allow you to create so-called compound statements. The if construct is a compound statement. The for loop is a compound statement. And, a number of semicolon-separated Python statements are compound statements.
A lambda function allows you to define a function in a single line. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y: x+y calculates the sum of the two argument values x+y in one line of Python code.
Problem: How to define a function in a single line of Python code?
Example: Say, you’ve got the following function in three lines. How to compress them into a single line of Python code?
def say_hi(*friends): for friend in friends: print('hi', friend) friends = ['Alice', 'Bob', 'Ann']
say_hi(*friends)
The code defines a function say_hi that takes an iterable as input—the names of your friends—and prints 'hi x' for each element x in your iterable.
The output is:
'''
hi Alice
hi Bob
hi Ann '''
Let’s dive into the different methods to accomplish this! First, here’s a quick interactive overview to test the waters:
Exercise: Run the code—is the output the same for all four methods?
In the following article, you’ll learn about each method in greater detail!
Challenge: Say, you want to have the list indices as keys and the list elements as values.
# Given a list:
a = ['Alice', 'Liz', 'Bob'] # One-Line Statement Creating a Dict:
d = dict(enumerate(a)) print(d)
# {0: 'Alice', 1: 'Liz', 2: 'Bob'}
The one-liner dict(enumerate(a)) first creates an iterable of (index, element) tuples from the list a using the enumeratefunction. The dict()constructor than transforms this iterable of tuples to (key, value) mappings. The indextuple value becomes the new key. The element tuple value becomes the new value.
Learn more about different one-liners in the context of dictionaries in this blog article:
Dictionary Comprehension is a concise and memory-efficient way to create and initialize dictionaries in one line of Python code. It consists of two parts: expression and context. The expression defines how to map keys to values. The context loops over an iterable using a single-line for loop and defines which (key,value) pairs to include in the new dictionary.
Per convention, you use one-line docstrings if the function, module, class, or method is obvious enough to warrant a short explanation—but nothing more. You can enclose the one-liner docstring within single quotes, double quotes, or even triple quotes. However, enclosing the one-liner docstring in a triple quote is the most Pythonic way.
For example, the following function can be easily understood. Therefore, a one-liner docstring is sufficient to describe its behavior:
def add(x, y): '''Add both arguments and returns their sum.''' return x + y print(add.__doc__)
# Add both arguments and returns their sum.
Summary: Download a file over the web by using the following steps in Python.
Import libary requests
Define URL string
Get file data from URL
Store file data in file object on your computer
Here’s how you can do this to download the Facebook Favicon (source):
At the beginning of our struggle with web scraping, you may have trouble downloading files using Python. However, the following article provides you with several methods that you can use to download, for example, the cover of a book from the page.
A generator function is a Pythonic way to create an iterable without explicitly storing it in memory. This reduces memory usage of your code without incurring any additional costs.
Problem: Can we write a one-line generator?
Here’s the code that accomplishes this:
print(sum(random.random() for i in range(1000)))
The code consists of the following parts:
The print() function prints the result of the expression to the shell.
The sum() function sums over all values in the following iterable.
The generator expression random.random() for i in range(1000) generates 1000 random numbers and feeds them into the outer sum() function without creating all of them at once.
This way, we still don’t store the whole list of 1000 numbers in memory but create them dynamically.
To update a global variable in one line of Python, retrieve the global variable dictionary with the globals() function, and access the variable by passing the variable name as a string key such as globals()['variable']. Then overwrite the global variable using the equal symbol, for example in globals()['variable'] = 42 to overwrite the variable with value 42.
The code first accesses all global variables using the globals() function that returns a dictionary mapping names to objects. You access the value associated to the key 'a'. The return value is the object to which global variable a points.
Step 1: Here’s an interactive browser-based shell:
The shell runs any Python program in your browser.
Step 2: Type the print function in your browser shell with opening and closing parentheses that are initially empty:
print()
The print() function takes a string and prints it to your shell. This way, you can generate outputs in your program. Think about it: a Python program is only a means to an end. It transform an input to an output. One way of creating an output is to print program values to the shell. In our hello world one-liner, the output is the textual data (=string) 'hello world'.
Step 3: Pass the 'hello world' string into the print() function between the parentheses.
print('hello world')
Congratulations, your first hello world one-liner is ready! Now, there’s only one thing left:
Step 4: Run the hello world one-liner program by hitting the “Run” symbol .
Can you see the output generated by your program? When running your program in the interactive shell, the result should be the following output:
One-line comments begin with the hash (#) character and reach to the end of the line. The newline character terminates the meaning of the comment—which is for the Python interpreter to ignore the commented text. A special case are inline comments that are used after a regular Python statement but before the newline character. The PEP 8 standard recommends to use them sparingly.
# This is a one-line comment print('hi') # This is an inline comment
Problem: How to assign a value to a variable if it is not equal to None—using only a single line of Python code?
Solution: A beautiful extension of Python 3.8 is the Walrus operator. The Walrus operator := is an assignment operator with return value. Thus, it allows you to check a condition and assign a value at the same time:
# Method 2
if tmp := get_value(): x = tmp
This is a very clean, readable, and Pythonic way. Also, you don’t have the redundant identity assignment in case the if condition is not fulfilled.
You create a list with three elements. Then, you create an anonymous function that takes one argument (an integer in our case) and increments it by one. The map function applies the function to each element in the list and returns a new map object. This is converted back to a list using the list(...) function.
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:
• Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.
Python’s dictionary data structure is one of the most powerful, most underutilized data structures in Python. Why? Because checking membership is more efficient for dictionaries than for lists, while accessing elements is easier for dictionaries than for sets.
In this tutorial, you’ll learn how to perform four common dictionary operations in one line of Python code. By studying these problems, you’ll not only learn how to use dictionaries in Python, but you’ll become a better coder overall. So, let’s dive into the first problem: creating a dictionary from a list in one line.
Python Create Dictionary From List in One Line
Challenge: Create a dictionary from a list in one line of Python.
Example: Say, you want to have the list indices as keys and the list elements as values.
# Given a list:
a = ['Alice', 'Liz', 'Bob'] # One-Line Statement Creating a Dict:
d = one-line-statement print(d)
# {0: 'Alice', 1: 'Liz', 2: 'Bob'}
Solution: There are multiple ways to accomplish this task. Let’s learn the most Pythonic one next:
# Given a list:
a = ['Alice', 'Liz', 'Bob'] # One-Line Statement Creating a Dict:
d = dict(enumerate(a)) print(d)
# {0: 'Alice', 1: 'Liz', 2: 'Bob'}
The one-liner dict(enumerate(a)) first creates an iterable of (index, element) tuples from the list a using the enumeratefunction. The dict()constructor than transforms this iterable of tuples to (key, value) mappings. The indextuple value becomes the new key. The element tuple value becomes the new value.
Try it yourself in our interactive code shell:
Exercise: Explore the enumerate() function further by printing its output!
Python One Line For Loop to Create Dictionary
Challenge: How to create a dictionary from all elements in a list using a single-line for loop?
Example: Say, you want to replace the following four-liner code snippet with a Python one-liner.
a = ['Alice', 'Liz', 'Bob'] data = {}
for item in a: data[item] = item
a = ['Alice', 'Liz', 'Bob'] # One-Liner Dictionary For Loop
data = {item:item for item in a} print(data)
# {'Alice': 'Alice', 'Liz': 'Liz', 'Bob': 'Bob'}
Dictionary Comprehension is a concise and memory-efficient way to create and initialize dictionaries in one line of Python code. It consists of two parts: expression and context. The expression defines how to map keys to values. The context loops over an iterable using a single-line for loop and defines which (key,value) pairs to include in the new dictionary.
You can learn about dictionary comprehension in my full video tutorial:
Challenge: How can you print a dictionary in a well-structured way using only a single line of Python code (without using multiple lines to create the output)?
Solution: Use the pretty print function!
The built-in module pprint contains the function pprint. This will ‘pretty print’ your dictionary. It sorts the keys alphabetically and prints each key-value pair on a newline.
from pprint import pprint
messy_dict = dict(z='Here is a really long key that spans a lot of text', a='here is another long key that is really too long', j='this is the final key in this dictionary') pprint(messy_dict) '''
{'a': 'here is another long key that is really too long', 'j': 'this is the final key in this dictionary', 'z': 'Here is a really long key that spans a lot of text'} '''
Printing the dictionary this way, doesn’t change the dictionary in any way but makes it more readable on the Python shell!
Iterate Over Dictionary Python One Line
Challenge: How to iterate over a dictionary in Python in one line?
Example: Say, you want to go over each (key, value) pair of a dictionary like this:
age = {'Alice': 19, 'Bob': 23, 'Frank': 53} # Iterate over dictionary (key, value) pairs
for name in age: key, value = name, age[name] print(key, value) '''
OUTPUT:
Alice 19
Bob 23
Frank 53 '''
But you want to do it in a single line of Python code! How?
Solution: Use the dict.items() method to obtain the iterable. Then, use a single-line for loop to iterate over it.
age = {'Alice': 19, 'Bob': 23, 'Frank': 53} # Iterate over dictionary (key, value) pairs
for k,v in age.items(): print(k,v) '''
OUTPUT:
Alice 19
Bob 23
Frank 53 '''
The output is the same while the code is much more concise. The items() method of a dictionary object creates an iterable of (key, value) tuple pairs from the dictionary.
Python Update Dictionary in One Line
Challenge: Given a dictionary and a (key, value) pair. The key may or may not already exist in the dictionary. How to update the dictionary in one line of Python?
Solution: Use the square bracket notation dict[key] = value to create a new mapping from key to value in the dictionary. There are two cases:
The key already existed before and was associated to the old value_old. In this case, the new value overwrites the old value_old after updating the dictionary.
The key didn’t exist before in the dictionary. In this case, it is created for the first time and associated to value.
Here’s the code:
age = {'Alice': 19, 'Bob': 23, 'Frank': 53} print(f"Alice is {age['Alice']} years old")
# Alice is 19 years old # Alice's Birthday
age['Alice'] = 20 print(f"Alice is {age['Alice']} years old")
# Alice is 20 years old
Challenge 2: But what if you want to update only if the key didn’t exist before. In other words, you don’t want to overwrite an existing mapping?
Solution: In this case, you can use the check if key in dict to differentiate the two cases:
age = {'Alice': 19, 'Bob': 23, 'Frank': 53} print(f"Alice is {age['Alice']} years old")
# Alice is 19 years old # Alice's Birthday
if 'Alice' not in age: age['Alice'] = 20 print(f"Alice is {age['Alice']} years old")
# Alice is 19 years old
Now, you may want to write this in a single line of code. You can do it the naive way:
setdefault(key[, default]) — If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
You can simply ignore the return value to update a key in a dictionary if it isn’t already present.
age = {'Alice': 19, 'Bob': 23, 'Frank': 53} print(f"Alice is {age['Alice']} years old")
# Alice is 19 years old # Alice's Birthday
age.setdefault('Alice', 20) print(f"Alice is {age['Alice']} years old")
# Alice is 19 years old
The age.setdefault('Alice', 20) only inserts the key 'Alice' if it isn’t already present (in which case it would associate the value 20 to it). But because it already exists, the command has no side effects and the new value does not overwrite the old one.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Python’s ImportError (ModuleNotFoundError) indicates that you tried to import a module that Python doesn’t find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH. If this file (__init__.py) is in the folder, change the position of the import in the file that is being imported from top to bottom.
Why does Python ImportError Occur?
An ImportError is detected when Python has problems with a successful module import. Usually this problem is caused by the incorrect path and is usually displayed with the message that there is “No module named (…)” or “cannot import name (…)”.
You can see an interactive example in our online browser project:
Exercise: Try to fix the error message by following the hint in the comment!
So what can we do with this error?
Creating Local Package
If the ImportError is raised (ImportError: No module named (…)), when you import your own script, you need to check if the script you want to import has a file named __init__.py in its directory, if it does not, then you need to create it, because files named __init__.py are used to mark directories on the disk as directories of Python packages, and directories without such file are ignored.
To add this file simply create a text document named __init__ in your folder and change its extension to .py => __init__.py.
Note: Remember that the __init__.py file cannot have any other characters in its name!!!
Adding Your Package To Path
When you want to add your module to the path permanently, you need to find the path to the site-packages folder and add the folder containing your module to this or another folder (where of course Python looks for modules).
The question is: How can the Path be found?
The easiest way to find the path is to write the following script:
Then we see all the paths in which Python looks for modules, just add your module to one of them (the best …\lib\site-packages). Once we do this, we will be able to call the module from any Python script.
When you have several files that import each other
Sometimes in Python, even if you have a __init__.py file in your folder, the ImportError occurs, it says that name cannot be imported. To eliminate this problem, the order of imports must be changed. The code causing the error:
#a2.py file
from test.b import b2
def a1(): print('a1') b2()
from test.a import a1 #b2.py file
def b1(): print('b1') a1()
def b2(): print('b2')
if __name__ == '__main__': b1()
Output will be the following – ImportError: cannot import name 'a1'. But if we change the position of from test.b import b2 in A like below:
def a1(): print('a1') b2()
from test.b import b2
Then we can get what we want:
b1
a1
b2
Summary
At the beginning we explained how to solve the problem from the title, and then we explained why the import error occurs. Then three ways of action were presented. First described how to make a local package (adding __init__.py), second how to make a package that can be called from any Python script (add the module to the site-packages folder) and third what to do when you have several files that import each other (ImportError: cannot import name (...)).
I hope this article helped you understand why this error occurred in your file and gave you a clue to remove it.
Summary: The UnicodeEncodeError generally occurs while encoding a Unicode string into a certain coding. Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode(utf-8) and decode(utf-8) functions accordingly in your code.
You might be using handling an application code that needs to deal with multilingual data or web content that has plenty of emojis and special symbols. In such situations, you will possibly come across numerous problems relating to Unicode data. But python has well-defined options to deal with Unicode characters and we shall be discussing them in this article.
What is Unicode?
Unicode is a standard that facilitates character encoding using variable bit encoding. I am sure, you must have heard of ASCII if you are into the world of computer programming. ASCII represents 128 characters while Unicode defines 221 characters. Thus, Unicode can be regarded as a superset of ASCII. If you are interested in having an in-depth look at Unicode, please follow this link. Click on Unicode:- U+1F40Dto find out what it represents! (Try it!!!)
What is a UnicodeEncodeError?
The best way to grasp any concept is to visualize it with an example. So let us have a look at an example of the UnicodeEncodeError.
u = 'é'
print("Integer value for é: ", ord(u))
print("Converting the encoded value of é to Integer Equivalent: ", chr(233))
print("UNICODE Representation of é: ", u.encode('utf-8'))
print("ASCII Representation of é: ", u.encode('ascii'))
Output
Integer value for é: 233
Converting the encoded value of é to Integer Equivalent: é
UNICODE Representation of é: b'\xc3\xa9'
Traceback (most recent call last): File "main.py", line 5, in <module> print("ASCII Representation of é: ",u.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 0: ordinal not in range(128)
In the above code, when we tried to encode the character é toits Unicode value we got an output but while trying to convert it to the ASCII equivalent we encountered an error. The error occurred because ASCII only allows 7-bit encoding and it cannot represent characters outside the range of [0..128].
You now have an essence of what the UnicodeEncodeError looks like. Before discussing how we can avoid such errors, I feel that there is a dire need to discuss the following concepts:
Encoding and Decoding
The process of converting human-readable data into a specified format, for the secured transmission of data is known as encoding. Decoding is the opposite of encoding that is to convert the encoded information to normal text (human-readable form).
In Python,
encode() is an inbuilt method used for encoding. Incase no encoding is specified, UTF-8 is used as default.
decode() is an inbuilt method used for decoding.
Example:
u = 'Πύθωνος'
print("UNICODE Representation of é: ", u.encode('utf-8'))
Output:
UNICODE Representation of é: b'\xce\xa0\xcf\x8d\xce\xb8\xcf\x89\xce\xbd\xce\xbf\xcf\x82'
The following diagram should make things a little easier:
Codepoint
Unicode maps the codepoint to their respective characters. So, what do we mean by a codepoint?
Codepoints are numerical values or integers used to represent a character.
The Unicode code point for é is U+00E9 which is integer 233. When you encode a character and print it, you will generally get its hexadecimal representation as an output instead of its binary equivalent (as seen in the examples above).
The byte sequence of a code point is different in different encoding schemes. For eg: the byte sequence for é in UTF-8 is \xc3\xa9 while in UTF-16 is \xff\xfe\xe9\x00.
Please have a look at the following program to get a better grip on this concept:
u = 'é'
print("INTEGER value for é: ", ord(u))
print("ENCODED Representation of é in UTF-8: ", u.encode('utf-8'))
print("ENCODED Representation of é in UTF-16: ", u.encode('utf-16'))
Output
INTEGER value for é: 233
ENCODED Representation of é in UTF-8: b'\xc3\xa9'
ENCODED Representation of é in UTF-16: b'\xff\xfe\xe9\x00'
Now that we have an overview of Unicode and UnicodeEncodeError, let us discuss how we can deal with the error and avoid it in our program.
Problem: Given a string/text to be written in a text File; how to avoid the UnicodeEncodeError and write given text in the text file.
Example:
f = open('demo.txt', 'w')
f.write('να έχεις μια όμορφη μέρα')
f.close()
Output
Traceback (most recent call last): File "uniError.py", line 2, in <module> f.write('να έχεις μια όμορφη μέρα') File "C:\Users\Shubham-PC\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>
Desired Output
Solution 1: Encode String Before Writing To File And Decode While Reading
You cannot write Unicode to a file directly. This will raise an UnicodeEncodeError. To avoid this you must encode the Unicode string using the encode() function and then write it to the file as shown in the program below:
text = u'να έχεις μια όμορφη μέρα'
# write in binary mode to avoid TypeError
f = open('demo.txt', 'wb')
f.write(text.encode('utf8'))
f.close()
f = open('demo.txt', 'rb')
print(f.read().decode('utf8'))
Output
Solution 2: Open File In utf-8
If you are using Python 3 or higher, all you need to do is open the file in utf-8, as Unicode string handling is already standardized in Python 3.
text = 'να έχεις μια όμορφη μέρα'
f = open('demo2.txt', 'w', encoding="utf-8")
f.write(text)
f.close()
Another approach to deal with the UnicodeEncodeError is using the codecs module.
Let us have a look at the following code to understand how we can use the codecs module:
import codecs f = codecs.open("demo3.txt", "w", encoding='utf-8')
f.write("να έχεις μια όμορφη μέρα")
f.close()
Output
Solution 4: Using Python’s unicodecsv Module
If you are dealing with Unicode data and using a csv file for managing your data, then the unicodecsv module can be really helpful. It is an extended version of Python 2’scsv module and helps the user to handle Unicode data without any hassle.
Since the unicodecsv module is not a part of Python’s standard library, you have to install it before using it. Use the following command to install this module:
$ pip install unicodecsv
Let us have a look at the following example to get a better grip on the unicodecsv module:
import unicodecsv as csv with open('example.csv', 'wb') as f: writer = csv.writer(f, encoding='utf-8') writer.writerow(('English', 'Japanese')) writer.writerow((u'Hello', u'こんにちは'))
Output
Conclusion
In this article, we discussed some of the important concepts regarding Unicode character and then went on to learn about the UnicodeEncodeError and finally discussed the methods that we can use to avoid it. I hope by the end of this article you can handle Unicode characters in your python code with ease.
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code.
We’ll look at a specific example of such an error: "typeerror: 'int' object is not iterable".
Exercise: Run this minimal example and reproduce the error in your online Python shell!
Let’s start decomposing this error step-by-step!
Background Integer & Iterable
First, it’s worth understanding what int and iterable are.
The int type in Python, as in almost all programming languages, is a type for storing integers such as 1, 2, 3, -324, 0. There can be many variables of type int in our program. We can assign values to them ourselves directly:
a = 5
In this case, we will most often understand what is a type of our variable. But the value can, for example, be returned from a function. Python uses implicit typing. Implicit typing means that when declaring a variable, you do not need to specify its type; when explicitly, you must. Therefore, by assigning the result of a function to a variable, you may not be clearly aware of what type your variable will be.
s1 = sum([1, 2, 3])
print(s1)
print(type(s1))
Output:
6
<class 'int'>
Here’s another example:
s2 = iter([1, 2, 3])
print(s2)
print(type(s2))
Output:
<list_iterator object at 0x7fdcf416eac8>
<class 'list_iterator'>
In this example, s1 is an integer and is of type int. This number is returned by the sum function with an argument in the form of a list of 3 elements. And the variable s2 is of type list_iterator, an object of this type is returned by the iter function, whose argument is the same list of 3 elements. We’ll talk about iteration now.
Iteration is a general term that describes the procedure for taking the elements of something in turn.
More generally, it is a sequence of instructions that is repeated a specified number of times or until a specified condition is met.
An iterable is an object that is capable of returning elements one at a time. It is also an object from which to get an iterator.
It seems that the easiest way to find out what exactly our function is returning is to look at the documentation.
So we see for the iter: iter(object[, sentinel]) Return an iterator object.
But for the sum we have nothing about a type of returning value. Check it out by yourself!
So, the typeerror: ‘int’ object is not iterable error occurs when the interpreter expects an iterable object and receives just an integer. Let’s consider the most common examples of such cases.
Invalid ‘sum’ Argument
We already wrote about the sum function. It returns the int value. The sum function takes at most two arguments. The first argument must be an object that is iterable. If it’s a collection of some sort, then it’s probably a safe assumption that it’s iterable. The second argument to the sum function is optional. It’s a number that represents the first number you’ll start adding to. If you omit the second argument, then you’ll start adding to 0. For novice Python programmers, it seems common sense that a function should return the sum of its arguments. Often they try to apply it like this:
a = 4
b = 3
sum(a, b)
Output:
TypeError Traceback (most recent call last) <ipython-input-12-35b280174f65> in <module>() 1 a = 4 2 b = 3
----> 3 sum(a, b) TypeError: 'int' object is not iterable
But we see that this leads to an error. We can fix this situation by pre-writing our variables for summation in an iterable object, in a list or a tuple, or a set, for example:
a = 4
b = 3 tuple_sum = (a, b)
list_sum = [a, b]
set_sum = {a, b}
dict_sum = {a: 0, b: 1} print(sum(tuple_sum))
print(sum(list_sum))
print(sum(set_sum))
print(sum(dict_sum))
Output:
7
7
7
7
As you can see, the result remains the same. Whether we are using pre-entry into a tuple, list, set, or even a dictionary. Note that for dictionaries, the sum function sums key values by default.
You can even write one variable to a list and calculate the sum of this list. As a search on stackoverflow shows, newbies in programming often try to calculate the sum of one element, which of course leads to an error.
a = 2
sum(a)
Output:
TypeError Traceback (most recent call last) <ipython-input-21-5db7366faaa2> in <module>() 1 a = 2
----> 2 sum(a) TypeError: 'int' object is not iterable
But if we pass an iterable object for example a list (even if it consists of one element) to the function then the calculation is successful.
a = 2
list_sum = [a]
print(sum(list_sum))
Output:
2
Another way to form such a list is to use the list.append method:
a = 2
list_sum = []
list_sum.append(a)
print('Sum of "a":', sum(list_sum))
b = 5
list_sum.append(b)
print('Sum of "a" and "b":',sum(list_sum))
Output:
'''
Sum of "a": 2
Sum of "a" and "b": 7 '''
Let’s consider a more complex version of the same error. We have a function that should calculate the sum of the elements of the list including the elements of the nested lists.
def nested_sum(list_): total = 0 for item in list_: item = sum(item) total = total + item return total
list1 = [1, 2, 3, [4, 5]]
print(nested_sum(list1))
Output:
TypeError Traceback (most recent call last) <ipython-input-35-c30be059e3a4> in <module>() 6 return total 7 list1 = [1, 2, 3, [4, 5]]
----> 8 nested_sum(list1) <ipython-input-35-c30be059e3a4> in nested_sum(list_) 2 total = 0 3 for item in list_:
----> 4 item = sum(item) 5 total = total + item 6 return total TypeError: 'int' object is not iterable
You can probably already see what the problem is here. The loop parses the list into its elements and goes through them. The items in our list are numbers 1, 2, 3 and a list [4, 5]. You can compute a sum of the list but you can’t get the sum of one number in Python. So we have to rewrite code.
def nested_sum(list_): total = 0 for item in list_: if type(item) == list: item = sum(item) total = total + item return total
list1 = [1, 2, 3, [4, 5]]
print(nested_sum(list1))
Output:
15
Now, in the loop, we first of all check the type of our local variable 'item' and if it is a list, then with a clear conscience we calculate its sum and rewrite the variable 'item' with the resulting value. If it’s just a single element, then we add its value to the 'total'.
Incorrect use of ‘for’ loop
Let’s consider another common case of this error. Can you see right away where the problem is?
n = 10
for i in n: print(i)
Output:
TypeError Traceback (most recent call last) <ipython-input-24-7bedb9f8cc4c> in <module>() 1 n = 10
----> 2 for i in n: 3 print(i) TypeError: 'int' object is not iterable
Perhaps the error in this construction is associated with the tradition of teaching children the Pascal language at school. There you can actually write something similar: for i:=1 to n do.
But in Python ‘for’ loops are used for sequential traversal. Their construction assumes the presence of an iterable object. In other languages, a ‘for each’ construct is usually used for such a traversal.
Thus, the ‘for’ construct in Python expects an iterable object which to be traversed, and cannot interpret an integer. This error can be easily corrected using the function ‘range’. Let’s see how our example would look in this case.
n = 10
for i in range(n): print(i)
Output:
0
1
2
3
4
5
6
7
8
9
The ‘range’ function can take 3 arguments like this: range(start, stop[, step]). The ‘start’ is the first number from which the loop will begin, ‘stop’ is the number at which the loop will end. Please note that the number ‘stop’ will not be included in the cycle. The ‘step’ is how much the number will differ at each next iteration from the previous one. By default, ‘start’ has a value of 0, ‘step’=1, and the stop parameter must be passed compulsory. More details with examples can be found in the documentation. https://docs.python.org/3.3/library/stdtypes.html?highlight=range#range
for i in range(4, 18, 3): print(i)
Output:
4
7
10
13
16
Here is a small example of using all three parameters of the ‘range’ function. In the loop, the variable ‘i’ in the first step will be equal to 4, ‘i’ will never be greater than or equal to 18, and will increase in increments of 3.
Problems With Tuples
The next example where an error "typeerror: ‘int’ object is not iterable" can occur is multiple assignment of values using a tuple. Let’s take a look at an example.
a, b = 0
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-6ffc3a683bb5> in <module>()
----> 1 a, b = 0 TypeError: 'int' object is not iterable
It’s a very pythonic way of assignment but you should be careful with it. On the left we see a tuple of two elements ‘a’ and ‘b’, so to the right of the equal sign there must also be a tuple (or any other iterable object) of two elements. Don’t be intimidated by writing a tuple without parentheses, this is an allowable way in Python.
So to fix this error, we can write the assignment like this:
a, b = 0, 0
print(a)
print(b)
Output:
0
0
And a few more examples of how you can assign values to several variables at once:
a, b = (1, 2)
c, d = {3, 4}
e, f = [5, 6]
print(a, b, c ,d ,e, f)
Output:
1 2 3 4 5 6
A similar problem can arise if you use a function that returns multiple values as a tuple. Consider, for example, a function that returns the sum, product, and result of division of two numbers.
def sum_product_division(a, b): if b != 0: return a + b, a * b, a / b else: return -1 sum_, product, division = sum_product_division(6, 2)
print("The sum of numbers is:", sum_)
print("The product of numbers is:", product)
print("The division of numbers is:", division)
Output:
The sum of numbers is: 8
The product of numbers is: 12
The division of numbers is: 3.0
Note that I have added an underscore to the variable name ‘sum_’. This is because the word ‘sum’ is the name of the built-in function that we discussed above. As you can see, in the case when ‘b’ is not equal to zero, our code works correctly, the variables take the appropriate values. Now let’s try to pass the value ‘b’ equal to 0 to the function. Division by zero will not occur, since we provided for this in the function and return -1 as an error code.
sum_, product, division = sum_product_division(6, 0)
print("The sum of numbers is:", sum_)
print("The product of numbers is:", product)
print("The division of numbers is:", division)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-6c197be50200> in <module>()
----> 1 sum_, product, division = sum_product_division(6, 0) 2 print("The sum of numbers is:", sum_) 3 print("The product of numbers is:", product) 4 print("The division of numbers is:", division) TypeError: 'int' object is not iterable
The error “TypeError: 'int' object is not iterable” occurs again. What’s the matter? As I already said, this situation is similar to the previous one. Here we also try to assign values to several variables using a tuple. But our function when there is a danger of division by zero returns not a tuple but the only value of the error code ‘-1’.
How to fix it? For example, we can check the type of a returning value. And depending on this type, already output the result. Let’s do it!
result = sum_product_division(6, 0)
if type(result) == int: print("Error, b should not be zero!")
else: sum_, product, division = result print("The sum of numbers is:", sum_) print("The product of numbers is:", product) print("The division of numbers is:", division)
Output:
Error, b should not be zero!
Here’s another example:
result = sum_product_division(6, 3)
if type(result) == int: print("Error, b should not be zero!")
else: sum_, product, division = result print("The sum of numbers is:", sum_) print("The product of numbers is:", product) print("The division of numbers is:", division)
Output:
The sum of numbers is: 9
The product of numbers is: 18
The division of numbers is: 2.0
We can also redesign our function to return the result of the operation from the beginning of the tuple. And use some trick when assigning variables. Take a look at this:
def sum_product_division(a, b): if b != 0: return "Ok", a + b, a * b, a / b else: return ("Error",) status, *results = sum_product_division(6, 0)
print(status, results) status, *results = sum_product_division(6, 2)
print(status, results)
Output:
Error []
Ok [8, 12, 3.0]
If division by zero is possible we return a tuple with a single element – the string ‘Error’. If everything is correct then we return a tuple where the first element is a status message – the string ‘Ok’ and then the results of the calculations follow sequentially: sum, product, result of division.
There can be many options here, because this is a function that we wrote ourselves, so we can fix it as we please. But it so happens that we use functions from libraries. For example here is an error from a topic on stackoverflow.
import subprocess
data = subprocess.call(["echo", '''Hello World!
Hello!'''])
sum_lines = 0
for line in data: print(line) sum_lines += 1
print(sum_lines)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-32-8d4cf2cb9ee0> in <module>() 3 Hello!''']) 4 sum_lines = 0
----> 5 for line in data: 6 print(line) 7 sum_lines += 1 TypeError: 'int' object is not iterable
I rewrote the code a bit so that the essence is clear. We want to run a command on the command line and count the number of lines printed on the screen. In our case, it will be just a command to display a little message to the World.
We should read a documentation to figure it out. The subprocess module allows you to spawn new processes, connect to their input /output /error pipes, and obtain their return codes. We see that the ‘call’ function starts the process on the command line, then waits for its execution and returns the execution result code! That’s it! The function returned the code of execution. It’s integer and we are trying to traverse this integer in a loop. Which is impossible, as I described above.
What to do? Explore the documentation for the module further. And so we find what we need. The ‘check_output’ function. It returns everything that should be displayed in the console when the command being passed is executed. See how it works:
import subprocess
data=subprocess.check_output(["echo", '''Hello World!
Hello!'''])
sum_lines = 0
for line in data.splitlines(): print(line) sum_lines +=1
print(sum_lines)
Output:
b'Hello World!'
b'Hello!'
2
Great! We got a byte string separated by newline symbols ‘\n’ at the output. And we can traverse over it as shown with a ‘splitlines’ function. It returns a list of the lines in the string, breaking at line boundaries. This method uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless ‘keepends’ parameter is given and true.
Thus, we fixed the error and got what we needed, but had to do a little research in the documentation. This study of documentation is one of the most effective ways to improve your programming skills.
The snag with lists
Often the error "TypeError: 'int' object is not iterable" appears when using various functions related to lists. For example I have a list of my exam grades. I want to add to it a grade in physical education which I passed perfectly in contrast to math. I am trying to do it like this:
I’m using the most conventional method to perform the list concatenation, the use of “+” operator. It can easily add the whole of one list behind the other list and hence perform the concatenation. But it doesn’t work here. Because list concatenation is only possible for two lists. We cannot combine list and number. The most obvious way to solve this problem is to use the ‘append’ function. It is designed just to do that. It adds an item to the list. The argument can also be an integer.
Voila! We did it! Of course, if we really want to use the ‘+’ operator, we can pre-write our physical education grade in a list with one element, for example like this:
The result is expectedly the same as the previous one. Move on.
Another list-related problem is when you’re trying to add element with ‘extend‘ method. This method can be very useful to concatenate lists. Unlike the ‘+’ operator, it changes the list from which it is called. For example, I need to add new semester grades to the grades list. It’s easy to do with the method ‘extend’:
Did you notice the difference? I originally defined the variable ‘physical_education_mark’ as a list with one item. And this works perfect!
Now suppose we need a function that will find the location of variables in the formula “A + C = D – 6”. If you know that each variable in the formula is denoted by one capital letter. We’re trying to write it:
def return_variable_indexes(formula): for element in formula: if element.isupper(): indexes = list(formula.index(element)) return indexes print(return_variable_indexes("A + C = D - 6"))
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-44-5a9b17ff47ae> in <module>() 5 return indexes 6 ----> 7 print(return_variable_indexes("A + C = D - 6")) <ipython-input-44-5a9b17ff47ae> in return_variable_indexes(formula) 2 for element in formula: 3 if element.isupper():
----> 4 indexes = list(formula.index(element)) 5 return indexes 6 TypeError: 'int' object is not iterable
Yes, we got the same error again. Let’s try to understand what’s the matter. We go through the elements of the string ‘formula’. And if this element is a upper-case letter then we use the ‘index’ function to find its position in the string. And try to write it into a list ‘indexes’. So we have two functions ‘index’ and ‘list’. What is returning value of the ‘index’ function? It is an integer number the position at the first occurrence of the specified value. So we’re trying to add this to the list ‘indexes’ with a ‘list’ function. And stop here! The ‘list’ constructor takes one argument. It should be an iterable object so that could be a sequence (string, tuples) or collection (set, dictionary) or any iterator object. Not an integer number of course. So we can use ‘append’ method again and get the result we need:
def return_variable_indexes(formula): indexes = [] for element in formula: if element.isupper(): indexes.append(formula.index(element)) return indexes print(return_variable_indexes("A + C = D - 6"))
Output:
[0, 4, 8]
And just for fun you can do it as a one-liner using a list comprehension and the ‘enumerate’ method. It takes iterable object as an argument and returns its elements with index as tuples (index, element) one tuple by another:
def return_variable_indexes(formula): return [index_ for index_, element in enumerate(formula) if element.isupper()]
print(return_variable_indexes("A + C = D - 6"))
Output:
[0, 4, 8]
Conclusion
We have considered some cases in which an error “TypeError: ‘int’ object is not iterable” occurs. This is always a situation where the interpreter expects an iterable object, and we provide it an integer.
The most common cases of such errors:
incorrect sum argument;
incorrect handling of tuples;
related to various functions and methods of lists
I hope that after reading this article you will never have a similar problem. And if it suddenly arises then you can easily solve it. You may need to read the documentation for this though =)
A generator function is a Pythonic way to create an iterable without explicitly storing it in memory. This reduces memory usage of your code without incurring any additional costs.
The following code shows a function get_numbers(n) that returns a list of nrandom numbers.
import random # NOT A GENERATOR!
# Create and return a list of numbers
def get_numbers(n): numbers = [] for i in range(n): numbers.append(random.random()) # List of n elements exists in memory return numbers # Sum up 1000 random numbers
s = 0
for x in get_numbers(1000): s += x
print(s)
However, this is not very efficient code because you create a list in advance without need. What if you had 1,000,000,000 numbers? Your memory would quickly fill up!
A better way is to use a generator function with the yield keyword that creates the random numbers dynamically as they are iterated over:
import random # GENERATOR
# Generate numbers one by one
def generate_numbers(n): for i in range(n): yield random.random() # Sum up 1000 random numbers
s = 0
for x in generate_numbers(1000): s += x
print(s)
There are two big advantages to using a generator:
(1) You don’t have to create a huge list first and store it in memory but generate the next element as you iterate over it.
(2) It’s shorter and more concise.
However, it may not be concise enough for you! So, here’s the problem addressed in this article:
Problem: Can we write a one-line generator?
Let’s dive into different methods to accomplish this!
Method 1: One-Liner Generator Function
print(sum(random.random() for i in range(1000)))
The code consists of the following parts:
The print() function prints the result of the expression to the shell.
The sum() function sums over all values in the following iterable.
The generator expression random.random() for i in range(1000) generates 1000 random numbers and feeds them into the outer sum() function without creating all of them at once.
This way, we still don’t store the whole list of 1000 numbers in memory but create them dynamically.
Method 2: exec()
The following method is not pretty—but it solves the problem to create a generator in a single line of code.
exec('def g(n):\n for i in range(n):\n yield random.random()')
The exec() function can be used to one-linerize every Python code snippet under the sun. Just pass the code you want to run as a string and replace all newlines with the newline character '\n'. This way, you can create a generator function g(n) that dynamically creates n random numbers. You can now iterate them using the standard code snippet:
s = 0
for x in g(1000): s += x
print(s)
# 488.318368852096
Because the numbers are random, the output will be different for you. You can try it yourself in our interactive shell:
Exercise: What’s the output for you? Why is it different than ours?
Python One-Liners Book
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:
• Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.
Problem: How to calculate the entropy with the SciPy library?
Solution: Import the entropy() function from the scipy.stats module and pass the probability and the base of the logarithm into it.
from scipy.stats import entropy p = [0.5, 0.25, 0.125, 0.125]
e = entropy(p, base=2) print(e)
# 1.75
Try It Yourself: Run this code in the interactive code shell!
Exercise: Change the probabilities. How does the entropy change?
Let’s start slowly! You’ll going to learn the most relevant background about entropy next.
Entropy Introduction
In thermodynamics, entropy is explained as a state of uncertainty or randomness.
In statistics, we borrow this concept as it easily applies to calculating probabilities.
When we calculate statistical entropy, we are quantifying the amount of information in an event, variable, or distribution. Understanding this measurement is useful in machine learning in many cases, such as building decision trees or choosing the best classifier model.
We will discuss the applications of entropy later in this article, but first we will dig into the theory of entropy and how to calculate it with the use of SciPy.
Calculating the Entropy
Calculating the information of a variable was developed by Claude Shannon, whose approach answers the question, how many “yes” or “no” questions would you expect to ask to get the correct answer?
Consider flipping a coin. Assuming the coin is fair, you have 1 in 2 chance of predicting the outcome. You would guess either heads or tails, and whether you are correct or incorrect, you need just one question to determine the outcome.
Now, say we have a bag with four equally sized disks, but each is a different color:
To guess which disk has been drawn from the bag, one of the better strategies is to eliminate half of the colors. For example, start by asking if it is Blue or Red. If the answer is yes, then only one more question is required since the answer must be Blue or Red. If the answer is no, then you can assume it is Green or Gray, so only one more question is needed to correctly predict the outcome, bringing our total to two questions regardless if the answer to our question is Green of Gray.
We can see that when an event is less likely to occur, choosing 1 in 4 compared to 1 in 2, there is more information to learn, i.e., two questions needed versus one.
Shannon wrote his calculation this way:
Information(x) = -log(p(x))
In this formula log() is a base-2 algorithm (because the result is either true or false), and p(x) is the probability of x.
As the higher the information value grows, the less predictable the outcome becomes.
When a probability is certain (e.g., a two-headed coin flip coming up heads), the probability is 1.0, which yields an information calculation of 0.
We can run Shannon’s calculation in python using the math library shown here:
When we change the probability to 0.25, as in the case of choosing the correct color of the disk, we get this result:
While it appears that the increase in information is linear, what happens when we calculate the roll of a single die, or ask someone to guess a number between 1 and 10? Here is a visual of the information calculations for a list of probabilities from less certain (p = 0.1) to certain (p = 1.0):
The graph shows that with greater uncertainty, the information growth is sub-linear, not linear.
Unequal Probabilities
Going back to the colored disks example, what if we now have 8 disks in the bag, and they are not equally distributed? Look at this breakout by color:
Color
Quantity
Blue
1
Green
1
Red
2
Gray
4
Total
8
If we use the original strategy of eliminating half of the colors by asking if the disk Blue or Green, we become less efficient since there is a combined 0.25 probability of either color being correct in this scenario.
We know that gray has the highest probability. Using a slightly different strategy, we first ask if Gray is correct (1 question), then move on to the next highest probability, Red (2nd question), and then to check if it is Blue or Green (3rd question).
In this new scenario, weighting our guesses will lead to less information required. The tables below show the comparison of the two methods. The info column is the product of the Probability and Questions columns.
Equal Guesses
Color
Prob
Q’s
Info
Blue
0.25
2
0.50
Green
0.25
2
0.50
Red
0.25
2
0.50
Gray
0.25
2
0.50
Total
1
8
2.00
Weighted Guesses
Color
Prob
Q’s
Info
Blue
0.125
3
0.375
Green
0.125
3
0.375
Red
0.25
2
0.50
Gray
0.5
1
0.50
Total
1
9
1.75
The Equal guess method takes an average of 2 questions, but the weighted guess method takes an average of 1.75.
We can use the Scipy library to perform the entropy calculation. Scipy’s “stats” sub-library has an entropy calculation that we can use. Here is the code to calculate the entropy for the scenario where the four disks have different probabilities:
The entropy method takes two entries: the list of probabilities and your base. Base=2 is the choice here since we are using a binary log for the calculation.
We get the same result as in the table shown above. With minimal code, the Scipy library allows us to quickly calculate Shannon’s entropy.
Further Uses
Entropy calculation is successfully used in real-world application in Machine Learning. Here are some examples.
Decision Trees
A Decision Tree is based on a set of binary decisions (True or False, Yes or No). It is constructed with a series of nodes where each node is question: Does color == blue? Is the test score > 90? Each node splits into two and decomposes into smaller and smaller subsets as you move through the tree.
Accuracy with your Decision Tree is maximized by reducing your loss. Using entropy as your loss function is a good choice here. At each step moving through the branches, entropy is calculated before and after each step. If the entropy decreases, the step is validated. Otherwise you must try another branch.
Classification with Logistic Regression
The key to a logistic regression is minimizing the loss or error for the best model fit. Entropy is the standard loss function for logistic regression and neural networks.
Code Sample
While there are several choices for using entropy as your loss function in machine learning, here is a snippet of code to show how the selection is made during model compilation:
Conclusion
The purpose of this article was to shed some light on the use of entropy with Machine Learning and how it can be calculated with Python.