Untitled
def add_lists_index_wise(list1, list2): # Use zip() to combine the two lists combined = zip(list1, list2) # Initialize an empty list to store the results result = [] # Iterate over the combined zip object for items in combined: # Add the elements together as strings and append to the result list result.append(''.join(items)) # If the input lists are of different lengths, append the remaining items result.extend(list1[len(list2):]) result.extend(list2[len(list1):]) return result # Test the function list1 = ["M", "na", "i", "Ke"] list2 = ["y", "me", "s", "lly"] print(add_lists_index_wise(list1, list2)) # Output: ['My', 'name', 'is', 'Kelly']
Leave a Comment