Python Forum

Full Version: Merge list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

how merge the first item of one list with the first item of another list.

first list:
D,B,C,

second list
5,2,7,

result should be

D5,B2,C7

I tried with the follow code, but the output are new group of list with two items.

list1 = IN[0]
list2 = IN[1]

merged_first_items = list(zip(list1, list2))
OUT = (merged_first_items)


Sorry, I am a beginner, my research on the web was not successful.

Best regards
a = ['D','B','C']

b = [5,2,7]

for index, c in enumerate(a):
    print(f'{c}{b[index]}')
With zip() it will be like this.
a = ['D', 'B', 'C']
b = [5, 2, 7]
merg_lst = zip(a, b)
new_list = [f'{x}{y}' for x, y in merg_lst]
print(new_list)
Output:
['D5', 'B2', 'C7']
a = ['D', 'B', 'C']
b = [5, 2, 7]
merg_lst = zip(a, b)
result = ','.join([f'{x}{y}' for x, y in merg_lst])
print(result)
Output:
D5,B2,C7