Table of Contents
Lists are one of the most used data structures in Python. In this article, we will discuss how to perform different operations on a list of strings in Python.
Create a List of Strings in Python
To create a list of strings, you can use the square brackets along with the string values as follows.
1 2 3 4 5 |
myList = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList) |
Output:
1 2 3 4 |
The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] |
Here, we have created a list of strings where each string is the name of a programming language.
If you have a container object like a set or tuple of strings, you can create the list of strings using the list()
constructor. The list()
constructor takes a container object as its input argument and returns a list of the elements present in the container object. From a set or tuple of strings, you can create a list of strings in python using the list()
constructor as follows.
1 2 3 4 5 6 7 8 |
mySet = {"Java", "Python", "C++", "C", "Javascript"} print("The set is:") print(mySet) myList = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList) |
Output:
1 2 3 4 5 6 |
The set is: {'Java', 'C', 'C++', 'Python', 'Javascript'} The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] |
In the above example, we have created a list of strings using a set of strings and the list()
constructor. The order of strings in the list is not the same in the list and set because a set is an unordered container object. Hence, the order in a set doesn’t matter. Due to this, the elements of the set aren’t added to the list in a specific order. Due to this, the order of elements in the list and the set are different.
Add String to a List of Strings in Python
To add a string to a list of strings, you can use the append()
method. The append()
method, when invoked on a list, accepts the string as the input argument and appends the input string at the end of the list of strings as shown below.
1 2 3 4 5 6 7 8 9 10 |
myList = ["Java", "Python", "C++", "C", "Javascript"] print("The original list is:") print(myList) newStr = "Typescript" print("The new string is:", newStr) myList.append(newStr) print("The updated list is:") print(myList) |
Output:
1 2 3 4 5 6 7 |
The original list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The new string is: Typescript The updated list is: ['Java', 'Python', 'C++', 'C', 'Javascript', 'Typescript'] |
To insert a string at a specified position in the list of strings, you can use the insert()
method. The insert()
method, when invoked on a list, takes the position of the element to be inserted as the first input argument and the element to be inserted as the second input argument. After execution, the element is inserted into the list at the specified position. In the following example, we have inserted a string at index 2 of the list of strings.
1 2 3 4 5 6 7 8 9 10 |
myList = ["Java", "Python", "C++", "C", "Javascript"] print("The original list is:") print(myList) newStr = "Typescript" print("The new string is:", newStr) myList.insert(2,newStr) print("The updated list is:") print(myList) |
Output:
1 2 3 4 5 6 7 |
The original list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The new string is: Typescript The updated list is: ['Java', 'Python', 'Typescript', 'C++', 'C', 'Javascript'] |
Concatenate List of Strings in Python
To concatenate the given list of strings in python, you can use the + operator as shown below.
1 2 3 4 5 6 7 8 9 10 11 |
myList1 = ["Java", "Python", "C++", "C", "Javascript"] print("The first list is:") print(myList1) myList2 = ["HTML", "CSS", "Markdown"] print("The second list is:") print(myList2) myList3 = myList1 + myList2 print("The concatenated list is:") print(myList3) |
Output:
1 2 3 4 5 6 7 8 |
The first list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The second list is: ['HTML', 'CSS', 'Markdown'] The concatenated list is: ['Java', 'Python', 'C++', 'C', 'Javascript', 'HTML', 'CSS', 'Markdown'] |
In the output, you can observe that the concatenated list contains elements from both the original list.
Further reading:
Check for a String in a List of Strings in Python
To check whether a string is present in the given list of strings, we can use the membership operator “in”
in python. The in
operator is a binary operator. It takes the element to be searched as the first operand and the container object as the second operand. After execution, it returns True
if the element is present in the container object. Otherwise, it returns False
. You can check for a string in a list of strings using the in
operator as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
myList1 = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList1) str1 = "C++" if str1 in myList1: print("{} is present in the list.".format(str1)) else: print("{} is not present in the list.".format(str1)) str1 = "Golang" if str1 in myList1: print("{} is present in the list.".format(str1)) else: print("{} is not present in the list.".format(str1)) |
Output:
1 2 3 4 5 6 |
The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] C++ is present in the list. Golang is not present in the list. |
Sort List of Strings in Python
To sort a list of strings, you can directly use the sort()
method. After execution, the elements of the list are sorted in alphabetical order. You can observe this in the following example.
1 2 3 4 5 6 7 8 |
myList1 = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList1) myList1.sort() print("The sorted list is:") print(myList1) |
Output:
1 2 3 4 5 6 |
The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The sorted list is: ['C', 'C++', 'Java', 'Javascript', 'Python'] |
If you are not allowed to modify the original list, you can use the sorted()
function as shown below.
1 2 3 4 5 6 7 8 |
myList1 = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList1) newList=sorted(myList1) print("The sorted list is:") print(newList) |
Output:
1 2 3 4 5 6 |
The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The sorted list is: ['C', 'C++', 'Java', 'Javascript', 'Python'] |
In the outputs above, you can observe that the lists have been sorted alphabetically.
Sort List of Strings According to the Length of Strings
To sort the list of strings according to the length of the strings, we can pass the len()
function as an input argument to the key parameter of the sort()
method. You can observe this in the following example.
1 2 3 4 5 6 7 8 |
myList1 = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList1) myList1.sort(key=len) print("The sorted list is:") print(myList1) |
Output:
1 2 3 4 5 6 |
The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The sorted list is: ['C', 'C++', 'Java', 'Python', 'Javascript'] |
Alternatively, you can also use the sorted()
function as shown below.
1 2 3 4 5 6 7 8 |
myList1 = ["Java", "Python", "C++", "C", "Javascript"] print("The list is:") print(myList1) newList = sorted(myList1, key=len) print("The sorted list is:") print(newList) |
Output:
1 2 3 4 5 6 |
The list is: ['Java', 'Python', 'C++', 'C', 'Javascript'] The sorted list is: ['C', 'C++', 'Java', 'Python', 'Javascript'] |
In the outputs above, you can observe that the lists have been sorted according to their length.
Conclusion
In this article, we have discussed and implemented different operations on a list of strings in python. I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!