Python | Joining unicode list elements

Sometimes we can receive data in different formats other than the conventional list or strings or integers or characters. Python has many other data types and knowledge of handling them is usually useful. This article demonstrates the joining of unicode characters in list. Let’s discuss certain ways in which this can be done.

Method #1 : Using join() + list comprehension
In this method, we first convert the unicode elements of strings to the string elements and then perform the join operation to get the joined resultant string from unicode elements list. Works in Python2 only.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# Joining unicode list elements
# using join() + list comprehension
  
# initializing list
test_list = [ 'We', 'love', 'Geeksforgeeks']
map(unicode, test_list)
  
# printing original list
print("The original list is : " + str(test_list))
  
# using join() + list comprehension to
# Join unicode list elements
res = b':'.join(str(i) for i in test_list)
  
# printing result
print ("The joined string is : " + res)

chevron_right


Output :



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

 

Method #2 : Using join() + str()
The list comprehension can be avoided as join function implicitly takes the joins of all the list elements and returns the joined unicodes which can then be converted to string using str function. Works in Python2 only.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Joining unicode list elements
# using join() + str()
  
# initializing list
test_list = [ 'We', 'love', 'Geeksforgeeks']
map(unicode, test_list)
  
# printing original list
print("The original list is : " + str(test_list))
  
# using join() + str() to
# Join unicode list elements
res = str(u':'.join(test_list))
  
# printing result
print ("The joined string is : " + res)

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.