Hi I am new to both Python and this forum.
My question:
I have two lists:
list_a = ['john','peter','paul']
list_b = [ 'walker','smith','anderson']
I succeeded in creating a list like this using zip
:
list_c = zip(list_a, list_b)
print list_c
# [ 'john','walker','peter','smith','paul','anderson']
But the result I am looking for is a list like:
list_d = ['john walker','peter smith','paul anderson']
Whatever I tried I didn't succeed! How may I get this result?
Python's extend() method can be used to concatenate two lists in Python. The extend() function does iterate over the passed parameter and adds the item to the list thus, extending the list in a linear fashion. All the elements of the list2 get appended to list1 and thus the list1 gets updated and results as output.
Using join() method to concatenate items in a list to a single string. The join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.
The most conventional method to concatenate lists in python is by using the concatenation operator(+). The “+” operator can easily join the whole list behind another list and provide you with the new list as the final output as shown in the below example.
You are getting zipped names from both the lists, simply join each pair, like this
print map(" ".join, zip(list_a, list_b))
# ['john walker', 'peter smith', 'paul anderson']
List_C = ['{} {}'.format(x,y) for x,y in zip(List_A,List_B)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With