1

I have a list of words as an output, data = ['1', '60s', '80s', 'metal', 'garage rock', 'adult alternative pop rock']. From this list, I would like to replace specific values with the value 'rock'. I have list of values that I want to replace with 'rock', namely rocklist = ['metal', 'garage rock', aldult alternative pop rock']. I want to delete the other values from the list.

I tried to do the following:

replace= ['rock' if x== rocklist else x for x in data]
``'print(replace)'''

but this didn't work. How should I go about this?

Scrambie
  • 41
  • 7

3 Answers3

3

you can do it with one list comprehension:

data = ['1', '60s', '80s', 'metal', 'garage rock', 'aldult alternative pop rock']
rocklist = ['metal', 'garage rock', 'aldult alternative pop rock']
data_filtered = [ i if i not in rocklist else 'rock' for i in data ]

returns :

['1', '60s', '80s', 'rock', 'rock', 'rock']
user_na
  • 2,154
  • 1
  • 16
  • 36
  • That code leaves the non-rock elements in the list. According to the OP, those should be removed. – inof Apr 22 '21 at 10:16
  • @inof for me it is not clear. I read it as he want to replace them and remove the origial ones. As op did not mention this too in his comment to my answer, I will leave the answer like this. – user_na Apr 22 '21 at 11:11
  • Ok … maybe the OP needs to clarify. To me it seemed clear: First “list of values […] to replace”, and then “delete the other values” (quoted from the question). It didn't occur to me that he could have meant it differently, but it’s certainly possible. – inof Apr 22 '21 at 13:11
2

You can use if else in your list comprehension if/else in a list comprehension

>>> data = ['1', '60s', '80s', 'metal', 'garage rock', 'adult alternative pop rock']
>>> rocklist = ['metal', 'garage rock', 'adult alternative pop rock']
>>> replace = [i if i not in rocklist else 'rock' for i in data]
>>> print(replace)
['1', '60s', '80s', 'rock', 'rock', 'rock']
Serhii
  • 1,367
  • 3
  • 13
  • 31
0
replace = ['rock' for x in data if x in rocklist]

Explanation: It’s a list comprehension that returns a list containing the string "rock" for each element of the list data that is contained in the list rocklist. In other words, it contains the string "rock" n times, where n is how often an element of rocklist occurs in data.

Note that the other elements (those that are not in rocklist) are not included in the resulting list, as the OP explained. Otherwise a ternary expression can be used to include those elements ('rock' if x in rocklist else x).

inof
  • 465
  • 3
  • 7
  • 1
    This won't include the "non-rock" elements – Throckmorton Apr 21 '21 at 18:15
  • @Throckmorton – That’s what the OP wants, actually: “the other values I want to delete from the list.” – inof Apr 21 '21 at 23:50
  • Please explain what your code does and how it does it. – M-Chen-3 Apr 22 '21 at 00:17
  • 1
    @M-Chen-3 – Actually the code should speak for itself. It’s a list comprehension that returns a list containing the string "rock" for each element of the list `data` that is contained in the list `rocklist`. In other words, it contains the string "rock" *n* times, where *n* is how often an element of `rocklist` occurs in `data`. In essence, the result is exactly what the OP described. In particular, note that the OP does *not* want elements to remain that are *not* in `rocklist` (see my other comment above). – inof Apr 22 '21 at 10:15
  • 1
    @inof Thanks for the explanation. You may think the code speaks for itself but an answer with an explanation is almost always more valued. See [this question](https://meta.stackoverflow.com/questions/405939/do-good-answers-require-an-explanation). – M-Chen-3 Apr 22 '21 at 15:41
  • 1
    @M-Chen-3 – Thanks for reminding me. You are right. I have added the explanation to the answer, so people don’t have to look through the comments in order to find it. – inof Apr 22 '21 at 18:05