Change language

Python | Joining Unicode List Items

|

Method # 1: Using join () + list comprehension
In this method, we first convert Unicode string elements to string elements and then execute a concatenation operation to get the concatenated resulting string from a list of Unicode elements. Only works in Python2.

# Python code for demonstration
# Attaching Unicode list items
# using join () + list comprehension

 
# initializing list

test_list = [ ’We’ , ’ love ’ , ’ Geeksforgeeks’ ]

map ( unicode , test_list)

 
# print the original about the list

print ( "The original list is: " + str (test_list))

 
# using join () + list comprehension for
# Join Unicode list items

res = b ’:’ . join ( str (i) for i in test_list)

 
# print result

print (  "The joined string is:" + res)

Output:

 The original list is: [’We’,’ love’, ’Geeksforgeeks’] The joined string is: We: love: Geeksforgeeks 

Method # 2: Using join () + str ()
The comprehension of the list can be avoided because the join function implicitly accepts the joins of all the elements of the list and returns the concatenated unicode which can then be converted to a string using the str function. Only works in Python2.

# Python3 demo code
# Attaching Unicode list items
# using join () + str ()

 
# initializing list

test_list = [ ’We’ , ’ love’ , ’Geeksforgeeks’ ]

map ( unicode , test_list)

 
# print original list < / code>

print ( "The original list is: " + str (test_list))

 
# using join () + str () for
# Join Unicode list items

res = str (u ’:’ . join (test_list))

 
# print result

print ( "The joined string is:" + res)

Output:

 The orig inal list is: [’We’,’ love’, ’Geeksforgeeks’] The joined string is: We: love: Geeksforgeeks