# Python – Create List of N Empty Strings

## Problem Formulation

💬 Challenge: Given an integer `n`. How to create a list of `n` empty strings `''` in Python?

Here are three examples:

• Given `n=0`. Create list `[]`.
• Given `n=3`. Create list `['', '', '']`.
• Given `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 `n` times.

```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 `list.append()` method.

```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))
# ['', '', '', '', '']
```

## Summary

There are three best ways to create a list with `n` empty strings.

1. List concatenation `[''] * n`
2. List comprehension `['' for _ in range(n)]`
3. Simple `for` loop with list `append('')` on an initially empty list

## Programmer Humor

`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!!