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.
# 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) |
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.
# 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) |
The original list is : ['We', 'love', 'Geeksforgeeks'] The joined string is : We:love:Geeksforgeeks
Recommended Posts:
- Python | Merge list of tuple into list by joining the strings
- Python | Joining only adjacent words in list
- Unicodedata – Unicode Database in Python
- Python | Merge List with common elements in a List of Lists
- Python | Sorting list of lists with similar list elements
- Python | Add list elements with a multi-list based on index
- Python | Replace elements in second list with index of same element in first list
- Python | Maximum sum of elements of list in a list of lists
- Python | Indices of sorted list of list elements
- Python | Index of Non-Zero elements in Python list
- Python | Get first and last elements of a list
- Python | Get last N elements from given list
- Python | List frequency of elements
- Python | Absolute value of list elements
- Python | Adding two list elements
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.



