0

Let's say I have a list that looks like this:

'CALSIM', '1556', '1897', '1358', '1321', '1254', '1403', '1124', '2561', '958', '712', '704', '562', '2092', '2020', '2001', '1890', '1748', 'Sky1', '1307', 'Sky29', '1802', '3429', '3522', 'Sky21'

I want to remove all instances of CALSIM and all instances of Sky# from my list. Is there a convenient method in Python?

Dila
  • 649
  • 1
  • 10
  • 1
    That's not a list. What have you tried yourself? Can you please share your code and explain where you're stuck exactly? [How to ask a good question](https://stackoverflow.com/help/how-to-ask) – Grismar Aug 01 '22 at 22:43
  • Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Nick Bailey Aug 01 '22 at 22:43
  • The usual way to do this is to create a new list that has only the items you want. – John Gordon Aug 01 '22 at 22:50

2 Answers2

0

You can add more conditions if you need.

strings = ['CALSIM', '1556', '1897', '1358', '1321', '1254', '1403', '1124', '2561', '958', '712', '704', '562', '2092', '2020', '2001', '1890', '1748', 'Sky1', '1307', 'Sky29', '1802', '3429', '3522', 'Sky21']
strings = [row for row in strings if row != "CALSIM" and not row.startswith("Sky")]

TaQ
  • 162
  • 1
  • 7
0

List comprehension:

[x for x in YOURLIST if x is not "CALSIM" and not x.startswith("Sky")]
hlin03
  • 125
  • 7