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

Time:01-06

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 i.e. ['apple','mango',lemon].

CodePudding user response:

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.

CodePudding user response:

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):])
  •  Tags:  
  • Related