简体   繁体   中英

Find out only the recently added items in the list (python)

Is there a way to find out only the recent additions to the list? For example:

list1=['apple', 'mango', 'banana']

Now few items are added to the list.

list2=['apple','mango','banana','apple','mango','lemon']

I need to keep only the most recent addition ie

['apple','mango','lemon'].

No, not without externally keeping track of those additions. Arrays have no concept of time - you can overwrite a value at any position in the array, or append to the end. You would need to cache recent additions somehow in a separate array.

Here you want recent elements as a list that you can get by indexing and slicing.

list1=['apple', 'mango', 'banana']
list2=['apple','mango','banana','apple','mango','lemon']


print(list2[len(list2) - len(list1):])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM