Add String to a List in Python

How to add a string or multiple strings to a list in Python? Adding a string to a list is a widespread usage of list operation and it can be done in several ways. For example, you can use the + operator, append(), insert(), extend(), and list comprehension. In this article, I will explain adding string to a list in python by using all these methods with examples.

Below are methods to add or append a string to a list in python:

  • Append String at the end of a list.
  • Add an element at a specific index in a list.
  • Add elements from the list to the end of the list.
  • Use + operator to concatenate two string lists together.
  • Use list comprehension to add string elements to a list.

1. Quick Examples of Adding String to a List

If you are in a hurry, below are some quick examples of adding a string to a list.


# Below are the quick examples

# Example 1: Add string to list at the end
# Using append() function
technology = ['Spark','Python','Pyspark']
technology.append('Java')

# Example 2: Add list with string to the list 
# Using append() function
technology1 = ['Hyperion', 'Pandas', 'Pyspark']
technology.append(technology1)

# Example 3: Add string at specific position
# Using insert() function
technology.insert(0, 'Hadoop')

# Example 4: Add string list by extending elements
# Using extend() function 
technology1 = ['Pandas', 'Pyspark']
technology.extend(technology1)

# Example 5: Use the + operator
technology += technology1

# Example 6: Using list comprehension
result = technology + [x for x in technology1]

2. Add String to List at the End

By using append() you can add a string at the end of the list in Python. This method takes the string as an argument and adds the string to the existing list, this does not return any value. For example, lets take list called technology and add the string Java using the following code.


# Consider list of strings
technology = ['Spark','Python','Pyspark']
print("Actual List",technology)

# Add string to the list
technology.append('Java')
print("Updated List: ",technology)

This example yields the below output.

python add string to list

Similarly, you can also use the append() to add a list of strings to a list in Python. For example, create a list technology1 and add it to the list technology. Now technology contains the elements of the original list and the list you added.


# Create two lists
technology = ['Java', 'Spark', 'Python']
technology1 = ['Hyperion', 'Pandas', 'Pyspark']
print("Original List 1: ",technology)
print("Original List 2: ",technology1)

# Add list and an element to list using append()
technology.append(technology1)
print("Updated List 1: ",technology)

This example yields the below output. Notice that the list is added as a single element resulting nested list.

python string elements list

3. Add String at a Specific Position

You can use the insert() to add a string at a specific index position in the python list. This takes two parameter, first the index where you watend to add and the string value to be added. Lets add string 'Hadoop' at the first position on the list. For more examples refer to add element to list by index.


# Add string at a specific position
technology = ['Spark','Python','Pyspark']

# Insert first position
technology.insert(0, 'Hadoop')
print(technology)

# Output:
# ['Hadoop', 'Spark', 'Python', 'Pyspark']

# Insert second position
technology = ['Spark','Python','Pyspark']
technology.insert(1, 'Hyperion')
print(technology)

# Output:
# ['Spark', 'Hyperion', 'Python', 'Pyspark']

4. Add Multiple Strings to the List

The extend() adds multiple strings to the existing list at the end. When you add an iterator with stirngs by using this method, it actually extends the strings as separate string and adds them to the list. Note that this modifies the existing list with the new strings and the strings are added at the end of the original list. For more examples refer to append multiple elements to the list.


# Add Multiple Strings to the List
technology = ['Spark', 'Python']
technology1 = ['Pandas', 'Pyspark']

# Extend list to list
technology.extend(technology1)
print(technology)

# Output:
# ['Spark', 'Python', 'Pandas', 'Pyspark']

Let’s add string from the tuple to insert into the list. Here, my list had 2 strings to start with, and added a tuple of 3 stirng, after adding a tuple, there are 5 string in the list.


# Consider the list of strings
technology = ['Spark', 'Python']
print("Actual List: ",technology)

# Extend tuple to list
technology.extend(('Java','Pandas','Pyspark'))
print("Final List: ",technology)

# Output:
# Actual List:  ['Spark', 'Python']
# Final List:  ['Spark', 'Python', 'Java', 'Pandas', 'Pyspark']

5. Add String to List Using + Operator

You can also use the "+" operator to concatenate two lists together. For example,


# Use the += operator
technology = ['Spark', 'Python']
technology1 = ['Pandas', 'Pyspark']
technology += technology1
print(technology)

# Output:
# ['Spark', 'Python', 'Pandas', 'Pyspark']

6. Using List Comprehension

You can use list comprehension to add a string to a list based on a certain condition or operation. For example,


# Using list comprehension
result = technology + [x for x in technology1]
print(result)

Conclusion

This article explained how to add or append a string at the end of a Python list, add a string at a specific index in a list, and add multiple stirngs from the list at the end. Also explained using + operator to concatenate two string lists together and finally using the list comprehension.

Happy Learning !!

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Add String to a List in Python