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?

0

3 Answers 3

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']
Sign up to request clarification or add additional context in comments.

3 Comments

That code leaves the non-rock elements in the list. According to the OP, those should be removed.
@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.
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.
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']

Comments

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).

6 Comments

This won't include the "non-rock" elements
@Throckmorton – That’s what the OP wants, actually: “the other values I want to delete from the list.”
@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 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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.