There is a list

lst = ['x', 'x123', 'x2345', 'x23461243'] def function(lst): for x in lst: for i in lst: if x in i: mass.remove(x) 

It is necessary to obtain a list of all values ​​in which x is present, and remove a single x from the list.

 returning_mass = ['x123', 'x2345', 'x23461243'] 

How to do it? The code above does not work correctly.

  • one
    Decide whether sorting or filtering is required? - mkkik
  • If we only have x, then we delete the element, and if x and something else is left and then we sort the list, right? - nick_gabpe
  • one
    returning_mass = [i for i in lst if i != 'x'] - gil9red

2 answers 2

 lst = ['x', 'x123', 'x2345', 'x23461243', '43286452378'] returning_mass = [i for i in lst if 'x' in i and i != 'x'] print(returning_mass) 

Slightly refined the answer @ gil9red, added a check that the 'x' is present in the array element.

     data = ['x', 'x123', 'x2345', 'x23461243', '43286452378'] mask = 'x' returning_mass = [i.replace(mask,'') for i in data if mask in i] print(returning_mass)