Append Multiple Strings to List in Python (3 Examples)
In this post, I’ll illustrate how to add strings to a list in Python programming.
The article consists of the following content:
Let’s dive right into the examples!
Have a look at the following example data.
my_list = ["Hello", "you!"] # create sample list
Above, I have created a sample list
my_list containing the strings
you!. Next, we will go over the alternative ways to add new strings to
Example 1: Add Multiple Strings to List using extend() Method
One mos common way of adding multiple elements to a list is to use the extend() method. Let’s see how it works!
my_list.extend(("I", "love", "coding.")) # extend my_list with new strings print(my_list) #print my_list # ['Hello', 'you!', 'I', 'love', 'coding.']
As seen, parsing the new strings
'coding.', we have extended
my_list with three extra strings.
Example 2: Add Multiple Strings to List using Concatenation Operator
An alternative to the extend() method is using the concatenation operator. In this method, first, we need to create a list including the strings to be added. Then we should use the concatenation symbol
+ to join this list with the original one. Check it out below!
my_list + ["It", "is", "great!"] # add new strings to my_list # ['Hello', 'you!', 'I', 'love', 'coding.', 'It', 'is', 'great!']
As observed, the previous attempt returned an extended list with the strings
Example 3: Add Multiple Strings to List using append() Method
The last alternative shown in this tutorial is using the append() method, which adds one element to a list at a time. In order to repeat the process for all elements to be added, we will set a for loop.
for s in ['What', 'about', 'you?']: # append strings in new_strings my_list.append(s) print(my_list) #print my_list # ['Hello', 'you!', 'I', 'love', 'coding.', 'What', 'about', 'you?']
This output shows that the early applied concatenation operation didn’t update
my_list. Please be aware that
my_list doesn’t contain the strings
'great!' while containing the first added strings
The reason is that the extend() method works in place, which means it modifies the object directly. On the other hand, the object remains the same when the concatenation operator is used.
You should explicitly assign the operation to the object back, like
my_list += ["It", "is", "great!"], if you like to update your list.
Video, Further Resources & Summary
Have a look at the following video tutorial on my YouTube channel. In the video, I explain the Python codes of this tutorial in a live programming session:
The YouTube video will be added soon.
Besides the video, you might have a look at the related tutorials on my website.
- Sort List of Strings in Python
- Append to 2D List in Python in R
- Append Multiple pandas DataFrames in Python in R
- Read Multiple CSV Files & Append into One pandas DataFrame in Python
- Introduction to Python
To summarize: In this Python programming article, you have learned how to extend a list with strings. In case you have further questions, tell me about it in the comments.
This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview of her other tasks on Statistics Globe.
Statistics Globe Newsletter