-3

Given an array:

 arr = ['a','b','c','d','a','b','d','f']

I would like to preprocess it with some kind of dictionary:

 dictionary = ['a','b','c']

so after: arr.preprocess(dictionary) all items not exisiting in dictionary will be deleted, arr will be now like:

['a','b','c','a','b']
heisenberg7584
  • 427
  • 9
  • 23

3 Answers3

2
[item for item in arr if item in dictionary]
mcsoini
  • 5,232
  • 1
  • 10
  • 36
2

Here is a possible solution:

arr = ['a','b','c','d','a','b','d','f']
dictionary = ['a','b','c']
arr = [x for x in arr if x in dictionary]
Riccardo Bucco
  • 11,231
  • 3
  • 17
  • 42
1

Verified Solution:

arr = ['a','b','c','d','a','b','d','f']
dictionary = ['a','b','c']
li = []
li = [element for element in arr if element in dictionary]
print(li)

Cheers

DRV
  • 510
  • 6
  • 17