Python join list means concatenating a list that has strings with a definite delimiter for making a string. Moreover, sometimes it’s way too useful when we have to change the list to a string. For example, converting a list of the alphabets to a string separated with commas to save in a file. Thus, we will learn about joining list in detail here.
Python Join List
We can use the python string join() function for joining a list of the strings. However, this function takes iterable as an argument and the list is iterable, so we are able to use it with the list.
Moreover, the list must comprise of the strings, if we will try joining list of ints then we will receive an error message as ‘TypeError’: sequence item 0: expected str instance, int found.
Parameters: The join() method takes iterable – objects capable enough to return their members one at a time. Some examples are as follows: List, Tuple, String, Dictionary, and Set.
Return Value: This method basically returns a string that is concatenated with the iterable’s elements.
Type Error: If the iterable comprises of any non-string values, it will raise a TypeError exception.
Let’s have a look at the short example for joining the list in Python to make a string:
vowels = ["a", "e", "i", "o", "u"]
vowelsCSV = ",".join(vowels)
print("Vowels are = ", vowelsCSV)
While we run the above program, it gives the following output:
Vowels are =Â a,e,i,o,u
Browse more Topics Under List Operations
Python joins two strings
We can apply join() function to join 2 strings too.
message = "Hello ".join("World")
print(message) #prints 'Hello World'
Why join() function is there in the String and not in the List?
One question comes to the mind of many python developers and that is: why the join() function is a part of the String and not of the list. Wouldn’t the syntax present below be easier to remember and use as well:
vowelsCSV = vowels.join(“,”)
Joining list of numerous data-types.
Let’s have a look at a program where we will join list items with numerous data types:
names = ['Java', 'Python', 1]
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))
Split String using the join function
We can use the join() function to split a string with the specified delimiter too.
names = 'Python'
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))
Using the split() function
Apart from splitting with the join() function, we can also use the split() function for splitting a string as well which will work almost the same way as the join() function works. Let’s have a look at the code snippet:
names = ['Java', 'Python', 'Go']
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))
split = single_str.split(delimiter)
print('List: {0}'.format(split))
Splitting only n times
The split() function that we demonstrated in the above example also takes an optional second argument which signifies the amount of times the split operation we should perform. Here’s a sample:
names = ['Java', 'Python', 'Go']
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))
split = single_str.split(delimiter, 1)
print('List: {0}'.format(split))
FAQs on Joining List
Question 1: How do we join a list?
Answer: For joining 2 lists:
Join two lists: list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list3 = list1 + list2. print(list3)
Append list2 into list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3] for x in list2: list1.append(x)
Use the extend() method to add list2 at the ending of the list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list1.extend(list2)
Question 2: Define, join() in Python.
Answer: Python String join():
The join() method delivers a flexible way for creating the strings from the iterable objects.
Leave a Reply