5 Best Ways to Extend a List of Strings in Python

πŸ’‘ Problem Formulation: When working with lists in Python, a common task is to add more items. Specifically, when you have a list of strings, you may need to append or insert additional strings to expand the collection. This article illustrates how to extend a list of strings in Python effectively. For example, suppose you have a list of fruits ['apple', 'banana'] and want to add 'cherry' to this list. The desired output would be ['apple', 'banana', 'cherry'].

Method 1: Using the append() method

The append() method is a straightforward way to add a single item to the end of a list. This method takes one argument, the string to be added, and directly modifies the original list in place.

Here’s an example:

fruits = ['apple', 'banana']
fruits.append('cherry')
print(fruits)

Output:

['apple', 'banana', 'cherry']

In this snippet, we start with a list named fruits that contains two items. By calling fruits.append('cherry'), we add ‘cherry’ to the end of the list. When we print fruits, we can see that the list now includes the new item.

Method 2: Using the extend() method

The extend() method allows you to add all elements of an iterable (like a list or tuple) to the end of the current list. Unlike append(), which adds its argument as a single element, extend() unfolds the iterable and adds each of its elements to the list.

Here’s an example:

fruits = ['apple', 'banana']
fruits.extend(['cherry', 'date'])
print(fruits)

Output:

['apple', 'banana', 'cherry', 'date']

In this code snippet, we use fruits.extend(['cherry', 'date']) to add both ‘cherry’ and ‘date’ to the list. The extend() method iterates over the list argument and adds each element to fruits.

Method 3: Using the + operator

Python also allows you to use the + operator to concatenate two lists. This operator is intuitive and can be used to quickly join a list with another list of strings.

Here’s an example:

fruits = ['apple', 'banana']
more_fruits = ['cherry', 'date']
fruits = fruits + more_fruits
print(fruits)

Output:

['apple', 'banana', 'cherry', 'date']

The + operator works by creating a new list that includes elements from both fruits and more_fruits. The original list fruits is then replaced by this new concatenated list.

Method 4: Using the append() method in a loop

If you want to add multiple strings individually and control the process, you might prefer looping through the items you want to add and calling append() for each one. This method gives more control over the addition process, such as conditional additions.

Here’s an example:

fruits = ['apple', 'banana']
new_fruits = ['cherry', 'date']
for fruit in new_fruits:
    fruits.append(fruit)
print(fruits)

Output:

['apple', 'banana', 'cherry', 'date']

This method iterates over each item in new_fruits and appends it to fruits using fruits.append(fruit). This approach allows for additional logic within the loop, giving more flexibility.

Bonus One-Liner Method 5: Using List Comprehension

A concise one-liner that utilizes list comprehension can be used to merge two lists. This technique is both powerful and compact, making it an appealing option for operations that can be expressed in a single line of code.

Here’s an example:

fruits = ['apple', 'banana']
fruits += [fruit for fruit in ['cherry', 'date']]
print(fruits)

Output:

['apple', 'banana', 'cherry', 'date']

List comprehension is used here to create a new list on the fly with [fruit for fruit in ['cherry', 'date']] and then it gets appended to the original fruits list using the += operator.

Summary/Discussion

  • Method 1 – append(): Simple and straightforward. Best for adding a single item. Cannot add multiple items at once.
  • Method 2 – extend(): Ideal for adding multiple items at once from an iterable. It directly modifies the original list.
  • Method 3 – + operator: Intuitive concatenation of two lists. However, it creates a new list, which may not be desirable for large lists due to memory considerations.
  • Method 4 – append() in a loop: Offers great control, allowing conditional or complex additions. Can be less efficient for large lists due to the loop overhead.
  • Method 5 – List Comprehension: Compact and Pythonic. Perfect for a one-liner addition when creating a new iterable inline is acceptable.