0

I have an array [1,2,3,4,5,6,1,2,3,4] and I would like to return 5, 6.

If I use set(my_array) I get 1,2,3,4,5,6. Is there a pythonic way to do this. Thanks.

Any help is much appreciated.

1
  • There are so many questions about counting occurrences (and indeed, ways to do so in Python) that I feel like this question was barely researched. It's almost certainly a duplicate. Commented Jun 8, 2016 at 15:32

2 Answers 2

2
#List of data which has every item repeated except for 5 and 6
lst= [1,2,3,4,5,6,1,2,3,4]

#This list comprehension prints a value in the list if the value only occurs once.
print [x for x in lst if lst.count(x)==1]
#Output
[5, 6]
Sign up to request clarification or add additional context in comments.

1 Comment

@ppperry I added a couple of comments to explain it more. I thought it was a fairly basic list comprehension.
1

You can use the appropriately named filter method:

>>> i = [1,2,3,4,5,6,1,2,3,4]
>>> filter(lambda x: i.count(x) == 1, i)
[5, 6]

Comments

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.