-5

I have a list which contains a mix of strings and numbers eg

old_list = [23, 35, string, 42, string]

I would like list to look like this

new_list = [23,35,45]

Is there a simple way to do this?

8
  • 2
    @Kasramvd: not quite, that filters on strings containing numbers.
    – Martijn Pieters
    Mar 31, 2016 at 8:26
  • 6
    Why users are answering a question that should be closed as "too broad"? This question doesn't meet the standards of Stack Overflow and I don't think we should encourage such questions.
    – Maroun
    Mar 31, 2016 at 8:28
  • 3
    @MarounMaroun: I don't see how this is too broad, frankly.
    – Martijn Pieters
    Mar 31, 2016 at 8:39
  • 3
    @MarounMaroun: 'lack of research' is not off topic. It is perfectly valid for a question to be down voted for that reason, but that doesn't make it closeable.
    – Martijn Pieters
    Mar 31, 2016 at 8:41
  • 2
    @MartijnPieters Yes, but the solutions are the same. Although your answer is more comprehensive than the answers in suggested question, you could close the question and post your answer on related question (or the duplicate which I'm sure has been asked already.) Anyway It's not a big deal, well done.
    – Mazdak
    Mar 31, 2016 at 8:41

2 Answers 2

6

Use a list comprehension to filter on type:

new_list = [value for value in old_list if isinstance(value, int)]

This picks out just integers. If you need to support arbitrary numbers (floats, complex numbers, decimal.Decimal instances) use the numbers.Number abstract type:

from numbers import Number

new_list = [value for value in old_list if isinstance(value, Number)]

You can also give isintance() a tuple of types to test against, if just need a a subset; for example:

new_list = [value for value in old_list if isinstance(value, (int, float))]

would filter on integers and floating point values.

4
  • Wouldn't [value for value in old_list if not isinstance(value, str)] cover all bases? Mar 31, 2016 at 8:38
  • @PadraicCunningham: it would only cover the base where the other values are strings. If you have more types then no, it wouldn't cover all bases. :-)
    – Martijn Pieters
    Mar 31, 2016 at 8:40
  • @Martin, I have a list which contains a mix of strings and numbers. I think the bases are well covered with not isinstance(value, str) ;) Mar 31, 2016 at 8:42
  • 1
    @PadraicCunningham: I prefer filtering on what you want, not on what you don't want. Either category could expand over time, but I find that what you don't want can expand to a far greater set to have to filter on. I rather nail down the smaller set of what you do want.
    – Martijn Pieters
    Mar 31, 2016 at 8:45
2

Apart form the already mentioned comprehension, you can consider the built-in filter function:

new_list = filter(lambda x: isinstance(x, int), old_list)
2
  • List comprehension should be preferred however. It's more commonly used.
    – knh170
    Mar 31, 2016 at 8:32
  • 1
    Note that in Python 3, filter returns an iterator, not a list. Of course, in many situations an iterator may be superior to having a list, but if you do want a list, eg, so you can index into it or count it, then you can do list(filter(lambda x: isinstance(x, int), old_list)).
    – PM 2Ring
    Mar 31, 2016 at 9:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.