How do i add two lists' elements into one list?

前端 未结 4 1093
执笔经年
执笔经年 2020-12-10 12:13

For example, I have a list like this:

list1 = [good, bad, tall, big]

list2 = [boy, girl, guy, man]

and I want to make a list like this:

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 12:38

    A solution using a loop that you try is one way, this is more beginner friendly than Xions solution.

    list3 = []
    for index, item in enumerate(list1):
        list3.append(list1[index] + list2[index])
    

    This will also work for a shorter solution. Using map() and lambda, I prefer this over zip, but thats up to everyone

    list3 = map(lambda x, y: str(x) + str(y), list1, list2);
    

提交回复
热议问题