Python - del element in list based on another list -
list1 = [1,2,3,4,5,6,7,8,9] list2 = [10,11,12,13,5,7]
and want list2 should cutted same elements in list1 , list2
--> list2 = [10, 11, 12, 13]
5 , 7 deleted because in list1.
this tried:
for in range(len(list1)): test = list1[i] if test in list2: del list2[list1[i]] print(list2)
but list2 same before :-(
hope can me edit: sorry forgot lists have got dates in datetime type. still work ?
try this, first cast both list
set
, can find differnce between 2 set
cast result list
, assign list2
:
list2 = list(set(list2)-set(list1)) list2 # [10, 11, 12, 13]
however works when dont have duplicates in lists.
Comments
Post a Comment