Creating a list from an existing list is a common task in Python programming. It’s often the case that you would need to create a new list based on the elements of an existing list, but perhaps with some modifications or filters applied.
π‘ Problem Formulation: Imagine you have a list of numbers or strings, and you want to create a new list that includes only certain elements from the original list or transforms those elements in a specific way. For instance, given a list input_list = [1, 2, 3, 4, 5]
, you want to create a new list output_list
where every element is the square of the numbers from input_list
.
Method 1: List Comprehension
List comprehension is a concise way to create lists in Python. It consists of brackets containing an expression followed by a for
clause, then zero or more for
or if
clauses. The expressions can be anything, meaning you can put in all kinds of objects in lists.
Here’s an example:
input_list = [1, 2, 3, 4, 5] output_list = [x ** 2 for x in input_list]
Output:

This one-liner loops through each element x
in input_list
, squares it with x ** 2
, and collects the results in a new list output_list
. Comprehensions are a powerful feature that allows creating a new list elegantly in a single, readable line.
π 7 Best Ways to Initialize a List of Lists in Python
Method 2: Using map() Function
map()
is a built-in Python function that applies a given function to all items in an input list and returns an iterator of the results. To create a list out of the results returned by map()
, you can wrap the map()
call with list()
.
Here’s an example:
input_list = [1, 2, 3, 4, 5] output_list = list(map(lambda x: x**2, input_list))
Output:

In this snippet, map()
takes a lambda function, which squares each element, and input_list
. The results are then converted into a list with list()
. It’s a functional programming approach that separates the function definition from the data processing.
Method 3: For Loop with Append
A for
loop combined with the list’s append()
method is a more verbose yet explicit way to create a list from another. You iterate each element of the input list and append the processed element to the new list.
Here’s an example:
input_list = [1, 2, 3, 4, 5] output_list = [] for item in input_list: output_list.append(item ** 2)
This code creates an empty list output_list
and iteratively processes each item
in input_list
by squaring it and appending the result to output_list
. This method is straightforward and easy for beginners to understand.
π How to Create a Python List?
Method 4: Using filter()
Sometimes, you want to create a list from another based on a condition, filtering out certain elements. The filter()
function builds a new list containing only items for which the function item returns true.
Here’s an example:
input_list = [1, 'hello', 3, 'world', 5] output_list = list(filter(lambda x: isinstance(x, str), input_list))
In this piece of code, filter()
is used with a lambda function that checks whether each element in input_list
is a string. filter()
returns only the strings, and list()
is used to convert the filter object to a list.
Bonus One-Liner Method 5: Using a Generator Expression
Like list comprehensions, generator expressions yield items on-the-fly rather than creating a whole list at once. This can be memory efficient for large lists. The generator expression can be wrapped with list()
to turn it into a list.
Here’s an example:
input_list = [1, 2, 3, 4, 5] output_list = list(x ** 2 for x in input_list)
This code snippet effectively does the same as the list comprehension method but creates a generator first. This method is used when dealing with huge datasets because it conserves memory.
Summary/Discussion
- List Comprehension: Concise, readable, good for simple transformations.
- Using
map()
Function: Functional programming style, good for applying a single transformation function. - For Loop with Append: Explicit, easy to understand, versatile for complex operations.
- Using
filter()
: Ideal for creating sublists based on a condition. - Bonus One-Liner Method 5 (Generator Expression): Memory efficient for large datasets, but slightly more complex.
List comprehensions and generator expressions are great for simple alterations and are memory-efficient. map()
and filter()
suit functional programming scenarios.
The for loop with append()
is versatile and can be preferable for more complicated scenarios where you need to check conditions or perform multiple operations on the elements.
π§βπ» Recommended: 10 Steps Learning Path: Becoming a Python Prompt Engineer π on the Finxter Academy