Jun-22-2018, 09:55 AM
(Jun-22-2018, 07:29 AM)volcano63 Wrote: [quote='DRPR' pid='50443' dateline='1529650625'] I am new to python and programming in general.I wanted to write a code which prints the similar elements from 2 lists .For example if a=[1,2,3,4,5] and b=[0,2,4,6] , I would want a c=[2,4]. Of course, experienced Pythonista would write it at least as[elem for elem in list1 if elem in list2]and there are couple of other ways to write it - but that will come in the future. Good luck with your studies!
I did the method for the experienced pythonista! but it returned a an error:
def simi1(list1,list2): c=[] [elem for elem in list1 if elem in list2] c.append(elem) return(c)
Error:NameError: name 'elem' is not defined
Any tips?