In this Python tutorial, we will see how to add a string to a list in Python with different methods and using practical examples.
In Python, a list is a mutable, or changeable, ordered sequence of elements. Each element inside a Python list is referred to as an item. These items can be of various data types, including strings, which are essentially sequences of characters.
Before we add a string to a list in Python, we first need to have a list. In Python, lists are created by placing a comma-separated sequence of items inside square brackets []. Here’s an example:
# A list of some major cities in the USA
usa_cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
In this example, usa_cities is a Python list that contains five strings, each representing a major city in the USA.
Add string in list Python
There are various different ways to add string to list in Python. They are:
- append() Method
- insert() Method
- extend() Method
- using the + Operator
- Using the * Operator for Unpacking
We will see them one by one using illustrative examples.
Method-1: Add string to a list in Python using append() method
The append() method in Python adds an item to the end of a list. It increases the list’s size by one. It offers a practical method to add one or more elements to a Python list that already exists.
For example, we have USA cities names in a Python list. Let’s try to add a new city in this Python list.
usa_cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
usa_cities.append('Philadelphia')
print(usa_cities)
The output is: The ‘Philadelphia’ is added in the end of the Python list.
['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia']
This way we can add a string to the list in Python using the append() method.
Method-2: Python add string to list using insert() method
The insert() method can add an item at a specific position in the Python list. The method takes two parameters: the index at which the item will be inserted, and the item itself.
For example, Let’s say we’re tracking the largest states in the USA by area:
large_states = ['Alaska', 'Texas', 'California']
large_states.insert(2, 'Montana')
print(large_states)
The output is:
['Alaska', 'Texas', 'Montana', 'California']
This way we can insert () method to add a string to the list in Python.
Method-3: Python list add string using extend() method
The Python’s List extends() method iterates over an iterable like a string, list, etc. If we have multiple strings that you want to add to the list, you can put them in another list and add them using the extend() method.
This method adds the elements of the supplied list to the end of the existing Python list. The list size will increase with the number of items we are adding.
For example, Suppose we have a list of some popular American food and we want to add more items to it:
usa_foods = ['Hot dogs', 'Hamburgers', 'Apple pie']
usa_foods.extend(['BBQ ribs', 'Fried chicken'])
print(usa_foods)
The Output is:
['Hot dogs', 'Hamburgers', 'Apple pie', 'BBQ ribs', 'Fried chicken']
This way we can use extend() method in the Python list to add strings.
Note: If we will add a string directly using extend() method, each letter will be considered an individual element. We can see below.
Read: How to replace items in a Python list
Method-4: Add string to list in Python using the + Operator
In Python, we can use the + operator to combine two lists. This method is straightforward and allows us to add multiple items at once. For concatenation, both items need be to of the same type.
The Python + operator will increase the size of the list by the total number of items in the other list and, the element will be added at the end of the list.
For example, Suppose we’re keeping a list of American Nobel laureates in Literature:
nobel_laureates = ['Ernest O. Lawrence', 'Linus Pauling']
nobel_laureates = nobel_laureates + ['Richard P. Feynman', 'Baruch S. Blumberg']
print(nobel_laureates)
The output is:
['Ernest O. Lawrence', 'Linus Pauling', 'Richard P. Feynman', 'Baruch S. Blumberg']
This way we can use the + operator to add the string to the list in Python.
Method-5: Add a string in list Python using the * operator for unpacking
The * operator can be used to unpack elements from an iterable (like a list or a string) and add them to another Python list. This operator is used to add elements of a string as separate items in a list.
For example, Let’s say we want to add each letter of the string ‘USA’ to a list:
letters = ['Z', 'F', 'C']
country = 'USA'
letters = [*letters, *country]
print(letters)
The Output is:
['Z', 'F', 'C', 'U', 'S', 'A']
This way we can use the * operator to add a string in the list Python.
Read: Append multiple items to list Python
Conclusion
Adding strings to a Python list is a fundamental operation in Python, and it is crucial for manipulating data within our code. The append(), insert(), and extend() methods provide straightforward ways to add one or more strings to a list, whereas we can use the + and * operator too. thereby enabling the dynamic modification of lists in Python.
You may like:
- How to compare two lists in Python
- remove the last element from the Python list
- delete an element from a Python list
- How to fix IndexError list out of Range Error in Python
- How to get the last element in the Python list
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.