0

I need help stripping integers from a list. For example, if I have the list [1,1,3,1,9,1,1,1,30] how could I extract the 3, 9 and 30?

2
  • 2
    please show your attempt Commented Mar 8, 2021 at 7:09
  • Please show a working piece of code you wrote yet and give specific error messages or details you would like help about. See MRE here: stackoverflow.com/help/minimal-reproducible-example
    – Malo
    Commented Mar 8, 2021 at 8:24

4 Answers 4

2

Thos could be accomplished with a list comprehension and a condition

list_of_ints = [1, 1, 3, 1, 9, 1, 1, 1, 30]
all_integers_except_ones = [i for i in list_of_ints if i != 1]
print(all_integers_except_ones)

outputs the list:

[3, 9, 30]
2
 l = [1,1,3,1,9,1,1,1,30]
[e for e in l if e != 1]

gives

[3, 9, 30]
1

I would use the filter() method with a lambda expression:

    mostly_ones = [1, 1, 3, 1, 9, 1, 1, 1, 30]
    list(filter(lambda x : x != 1, mostly_ones))

Output:

[3, 9, 30]
1
  • Also works well so upvoted. But the pythonic way of doing it would be to use a list comprehension :)
    – tbjorch
    Commented Mar 8, 2021 at 7:41
0

your question in not complete, but what I understood is that you want to remove the duplication from the list. well if you want to do that, just create and new empty list and append any item that is not in the new list to it.

list1 = [1,1,3,1,9,1,1,1,30]

new_list = [ ]

for item in list1:

if item not in new_list:
    new_list.append (item)

print (new_list)

or you can use count() method and remove() method to Remove the duplication.

1
  • No, OP doesn't want to remove duplication from the list, he wants to keep everything except ones (1).
    – Fareanor
    Commented Mar 8, 2021 at 16:04

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