💬 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
You can create a list of n empty strings by using list comprehension statement
['' for _ in range(n)] that uses the
range() function to repeat the creation and addition of an empty string
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()
To create a list of
n empty strings without special Python features, you can also create an empty list and use a simple
for loop to add one empty string at a time using the
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.
Get your Python One-Liners on Amazon!!
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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.