How to join a list in Python?

A list is a built-in data type in python, that holds a group of elements enclosed within [] brackets. The elements in the list can be concatenated by using the join() function, for loop, etc.

Consider a list of strings list_1 = ['a', 'b', 'c']. The joining of elements in list_1 with a hyphen character '-' returns the output as 'a-b-c'. This article discusses several ways to join the elements of the list.

Joining a list using the join() method

The join() function joins all the items in the list with a separator. The join() method takes a sequence(list, tuple, set, etc) as a parameter and returns a string. The syntax for the join() method is given below.

#syntax:
sep.join(iterable)

Consider a list list_1 = ["Welcome", "To", " Chercher", "Tech"]. ' '.join(list_1) uses '-' as a separator and returns a string as Welcome-To- Chercher-Tech as output.

list_1 = ["Welcome", "To", " Chercher", "Tech"]
print("list_1: ", list_1)
output = '-'.join(list_1)
print("Output is: ",output)

Output

list_1:  ['Welcome', 'To', ' Chercher', 'Tech']
Output is:  Welcome-To- Chercher-Tech
All the elements in the list must be string data type to join all the items in a list. Otherwise, the join() function raises a TypeError exception.

For example, Consider a list list_1 = ['Welcome', 'To', 'Python', 3]. The list contains an element 3 of integer data type. The join() function raises a TypeError exception when the below program is compiled.

list_1 = ["Welcome", "To", "Python", 3]
print("list_1: ", list_1)
output = ' '.join(list_1)
print("Output is: ",output)

Output

output = ' '.join(list_1)
TypeError: sequence item 3: expected str instance, int found

If the list contains elements of different data types, then it should be converted to string data type explicitly. A map() function can convert all the items in the list to string data type.

The map() function has two parameters, a function and an iterable. The map() function passes each data item of the iterable as an argument to the function returns an iterable as output.

Consider a list list_1 = ['Welcome', 'To', 'Python', 3]. The list contains an element 3 of integer data type. The map(str, list_1) converts all the elements to string data type. ' '.join(map(str, list_1)) uses white space ' ' as separator and returns output as Welcome To Python 3.

list_1 = ["Welcome", "To", "Python", 3]
print("list_1: ", list_1)
output = ' '.join(map(str, list_1))
print("Output is: ",output)

Output

list_1:  ['Welcome', 'To', 'Python', 3]
Output is:  Welcome To Python 3

Joining a list using for loop

Consider a list list_1 = ["Welcome", "To", "Python"]. The variable output and sep are initialized to list_1[0] and '-' respectively. A for loop traverses the list from the range 1 to the length of the list. At each iteration, the output is concatenated with the separator and the item in the list.

list_1 = ["Welcome", "To", "Python"]
print("list_1: ", list_1)
output = list_1[0]
sep = '-'
for i in range(1, len(list_1)):
    output += sep + list_1[i]
print("output is: ", output)

The above code returns the output as

list_1:  ['Welcome', 'To', 'Python']
output is:  Welcome-To-Python

The element in the list must be of string data type. Otherwise, the concatenation of a string and other data type elements leads to a TypeError exception. If the list contains data items other than string, then items in the list should be converted to string data type explicitly.

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions