| by Arround The Web | No comments

Python Add String to List

Sometimes, while working with Python, users encounter problems which it is required to add elements to a list. Python lists are a common data type that is used to store data. It can contain different data types and is also mutable. On the other hand, the Python strings are inside the quotes either single or double.

This post illustrated the multiple techniques to add the string to the list.

How to Add String to List in Python?

To add a string to the list, the following methods are used:

Method 1: Add String to List in Python Using “+” Operator

The easiest way to add the string to the list is the “+” operator which is used to convert the provided string into the list in Python.

Example

First, declare and initialize the list with numeric values:

myList = [1, 2, 3, 4]

Then, create a string type variable and pass a string:

testString = 'five'

Now, call the “print()” statement to check the element of the list as well as the value of the string variable:

print("Original List : " + str(myList))

print("Original String : " + str(testString))

Then, use the “+” operator to add the string to the list as an element:

myList += [testString]

Finally, use the print statement to check the final result of the list:

print("The List after Adding String : " + str(myList))

Output

Method 2: Add String to List in Python Using “insert()” Method

Another easiest method to convert any Python string into a Python list is the “insert()” method. It is used to add/insert the element at the end of the provided Python list by utilizing its length as the index number.

Example

Use the “len()” function to get the length of the list and store it in the “indexnum” variable:

indexNum = len(myList)

Call the “insert()” function along with the required string and list the index as a parameter:

myList.insert(indexNum, testString)

To get the resultant list, use the print statement:

print("The List after Adding String : " + str(myList))

As you can see, the provided string has been added to the list as its element successfully:

Method 3: Add String to List in Python Using “extend()” Method

The “extend()” method is also utilized for adding the string into the list in Python by merging one list with the last of the other list.

Example

Call the “extend()” method along with the required string as a list that needs to add to the list as a parameter:

myList.extend([testString])

Use the “print()” function to display the required result:

print("The List after Adding String : " + str(myList))

Output

Method 4: Add String to List in Python Using “itertool.chain()” Method

Python has multiple libraries which contain different functions for performing several operations. The “itertools” is one of them. It includes the “itertools.chain()” method which can be used for concatenating the provided list with any string element.

Example

First, import the “itertools” library:

Import itertools

Then, call its “itertools.chain()” method and pass a list and string variable as a list to it:

myList = list(itertools.chain(myList, [testString]))

To show the result, use the print statement:

print("The List after Adding String : " + str(myList))

Output

That’s all! We have described the different ways of adding strings to the list.

Conclusion

To add the string to the list, the “+” operator, the “insert()” method, the “extend()” method, and the “itertool.chain()” method can be used. All methods and operators are Python’s built-in functionalities. This post illustrated the multiple techniques to add the string to the list.

Share Button

Source: linuxhint.com

Leave a Reply