You could use a Counter
to count the elements in the list. Since you know there’s exactly one element that appears once, it will be the least common element, so:
def singleNumber(nums):
return Counter(nums).most_common()[-1][0]
More Related Contents:
- Access nested dictionary items via a list of keys?
- Writing a list to a file with Python
- Find the most common element in a list
- Splitting a list into N parts of approximately equal length
- Finding median of list in Python
- How can I verify if one list is a subset of another?
- Deleting multiple elements from a list
- Iterating through list of list in Python
- Split a list into parts based on a set of indexes in Python
- Find first sequence item that matches a criterion [duplicate]
- How do I iterate through two lists in parallel?
- Unable to reverse lists in Python, getting “Nonetype” as list
- How to return a subset of a list that matches a condition [duplicate]
- Assigning values to variables in a list using a loop
- Python – How to sort a list of lists by the fourth element in each list? [duplicate]
- How to get all combination of n binary value? [duplicate]
- Convert a columns of string to list in pandas
- Indexing a list with an unique index
- How do I split a string into a list of words?
- Why doesn’t assigning to the loop variable modify the original list? How can I assign back to the list in a loop? [duplicate]
- Sliding window maximum in O(n) time
- Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?
- Sorting list according to corresponding values from a parallel list [duplicate]
- Does Python have an immutable list?
- How to sort a 2D list? [closed]
- Counting depth or the deepest level a nested list goes to
- Which is the most efficient way to iterate through a list in python?
- How does str.startswith really work?
- Best way to remove elements from a list
- All possible permutations of a set of lists in Python