Common elements between two lists with no duplicates

后端 未结 5 1344
时光说笑
时光说笑 2021-01-16 05:19

Problem is this, take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
         


        
5条回答
  •  走了就别回头了
    2021-01-16 05:59

    The below code would work:

    newlist = []
    for x in b:
        if x in a:
            if x in newlist:
                print("duplicate")
            else:
                newlist.append(x)
    
    for y in newlist:
        print(y)   
    

提交回复
热议问题