Duplicate each element in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Duplicate each element in a List in Python #

To duplicate each element in a list:

  1. Declare a new variable that stores an empty list.
  2. Use a for loop to iterate over the original list.
  3. Use the list.extend() method to append each item twice to the new list.
main.py
my_list = ['bobby', 'hadz', 'com'] # ✅ duplicate each element in a list (using for loop) new_list = [] for item in my_list: new_list.extend([item, item]) # 👇️ ['bobby', 'bobby', 'hadz', 'hadz', 'com', 'com'] print(new_list) # ----------------------------------------------------- # ✅ duplicate each element in a list (using list comprehension) new_list = [item for item in my_list for _ in range(2)] # 👇️ ['bobby', 'bobby', 'hadz', 'hadz', 'com', 'com'] print(new_list)

The first example uses a for loop to duplicate each element in a list

The first step is to declare a new variable that stores an empty list.

main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [] for item in my_list: new_list.extend([item, item]) # 👇️ ['bobby', 'bobby', 'hadz', 'hadz', 'com', 'com'] print(new_list)

On each iteration, we use the list.extend() method to add each item of the original list twice to the new list.

The list.extend method takes an iterable and extends the list by appending all of the items from the iterable.

main.py
my_list = ['bobby'] my_list.extend(['hadz', '.', 'com']) print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']

The list.extend method returns None as it mutates the original list.

Alternatively, you can use a list comprehension.

Duplicate each element in a List using a list comprehension #

To duplicate each element in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the range() class to iterate twice for each item.
  3. Each element will be repeated in the new list.
main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [item for item in my_list for _ in range(2)] # 👇️ ['bobby', 'bobby', 'hadz', 'hadz', 'com', 'com'] print(new_list)

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

We also use the range() class to iterate twice for each list item.

The range class is often used for looping a specific number of times.

main.py
for n in range(2): print(n) # 👉️ 0 1 result = list(range(2)) # 👇️ [0, 1] print(result)

We only want to repeat each element twice, so we used a range of 2.

The _ character is used as a placeholder.

main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [item for item in my_list for _ in range(2)] # 👇️ ['bobby', 'bobby', 'hadz', 'hadz', 'com', 'com'] print(new_list)

There is a convention of naming variables that we don't intend to use as _.

Which approach you pick is a matter of personal preference. I'd go with using a for loop because I find it more intuitive and easier to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.