belongs to collection: Python List Exercises
Write a program to add two lists index-wise. Create a new list that contains the 0th index item from both the list, then the 1st index item, and so on till the last element. any leftover items will get added at the end of the new list.
Given:
list1 = ["M", "na", "i", "Ke"] list2 = ["y", "me", "s", "lly"]
Expected output:
['My', 'name', 'is', 'Kelly']
Hint:
Use list comprehension with the zip() function
zip()
Solution:
Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.
list1 = ["M", "na", "i", "Ke"] list2 = ["y", "me", "s", "lly"] list3 = [i + j for i, j in zip(list1, list2)] print(list3)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
Use list comprehension with the
zip()
functionSolution:
Use the
need an explanation for this answer? contact us directly to get an explanation for this answerzip()
function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.