Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge list
#1
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
Reply
#2
a = ['D','B','C']

b = [5,2,7]

for index, c in enumerate(a):
    print(f'{c}{b[index]}')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List of lists - merge sublists with common elements medatib531 1 4,096 May-09-2021, 07:49 AM
Last Post: Gribouillis
  how to merge a list of *.jpg and *.docx into one single PDF file ? smallabc 0 2,845 Feb-23-2020, 05:16 PM
Last Post: smallabc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020