-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
  • 563
  • 2
  • 10
  • 30

3 Answers3

2
[item for item in arr if item in dictionary]
mcsoini
  • 6,280
  • 2
  • 15
  • 38
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
  • 13,980
  • 4
  • 22
  • 50
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
  • 676
  • 1
  • 8
  • 22