💬 Challenge: Given an integer
n. How to create a list of
n empty strings
'' in Python?
Here are three examples:
n=0. Create list
n=3. Create list
['', '', ''].
n=5. Create list
['', '', '', '', ''].
Method 1: List Multiplication
You can create a list of
n empty strings using the list concatenation (multiplication) operator on a list with one empty string using the expression
[''] * n. This replicates the same identical empty string object to which all list elements refer. But as strings are immutable, this cannot cause any problems through aliasing.
def create_list_empty_strings(n): return [''] * n print(create_list_empty_strings(0)) #  print(create_list_empty_strings(3)) # ['', '', ''] print(create_list_empty_strings(5)) # ['', '', '', '', '']
Method 2: List Comprehension
def create_list_empty_strings(n): return ['' for _ in range(n)] print(create_list_empty_strings(0)) #  print(create_list_empty_strings(3)) # ['', '', ''] print(create_list_empty_strings(5)) # ['', '', '', '', '']
Method 3: For Loop and append()
def create_list_empty_strings(n): my_list =  for i in range(n): my_list.append('') return my_list print(create_list_empty_strings(0)) #  print(create_list_empty_strings(3)) # ['', '', ''] print(create_list_empty_strings(5)) # ['', '', '', '', '']
There are three best ways to create a list with
n empty strings.
- List concatenation
[''] * n
- List comprehension
['' for _ in range(n)]
forloop with list
append('')on an initially empty list
Thanks for reading this article with Finxter! ❤️
Question: How did the programmer die in the shower? ☠️
❗ Answer: They read the shampoo bottle instructions:
Lather. Rinse. Repeat.
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Liners will 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 (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) 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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.