Comparing two lists in Python

后端 未结 4 584
旧时难觅i
旧时难觅i 2020-12-30 10:41

So to give a rough example without any code written for it yet, I\'m curious on how I would be able to figure out what both lists have in common.

Example:

         


        
4条回答
  •  攒了一身酷
    2020-12-30 11:10

    If the two lists are the same length, you can do a side-by-side iteration, like so:

    list_common = []
    for a, b in zip(list_a, list_b):
        if a == b:
            list_common.append(a)
    

提交回复
热议问题