-2

I have to find all list elements are equal.

I have used below code:

        all(x == list[0] for x in list)

Now I have to find all list values are different. Please help me how to achieve this.

     List = ['a', 'b', 'x', 'y']

     Expected Output: True

     List = ['a', 'a', 'x', 'y']

     Expected Output: False

     List = ['a', 'a', 'a', 'a']

     Expected Output: False

Tried below code:

 list = ["aa", "bb", "bb"] 
 len(set(list)) != 1 
 output:
 will result True.
 I am expecting False, reason bb is same elements repeated 
Sagar
  • 59
  • 4
  • 11
  • @Aran-Fey, That is for to find out unique in list. That is i achieved it. I am looking for How to find all values are different in list – Sagar Apr 21 '18 at 08:59
  • 2
    What's the difference between "unique" and "different"? – Aran-Fey Apr 21 '18 at 08:59
  • if the length of your list is the same as the length of all uniques in your list ... ` all(x == list[0] for x in list)` could also be expressed as `len(set(list)) == 1` - and you should refrain from calling any variable ``list`, `set` or any other of the build-ins – Patrick Artner Apr 21 '18 at 09:00
  • @Aran-Fey I am confused. – Sagar Apr 21 '18 at 09:01
  • @Patrick Artner, if list = ["aa", "bb", "bb"] .........len(set(list)) != 1 ...will return True and I am expecting False – Sagar Apr 21 '18 at 09:11
  • `len(set(item)) == len(item)` if the lenght of your list is the same as the length of all unique elements of the list ... your list is unique to begin with... `len(set(list)) ==1` is: all elements of your list are the same - which you solved by `all(x == list[0] for x in list)`. `len(set(list)) != 1` checks if the list is empty or contains at least 2 different things ... – Patrick Artner Apr 21 '18 at 09:16
  • @Sagar if len(set(list)) != len(list) False else True – Roushan Apr 21 '18 at 09:22

2 Answers2

3

Use set filter the duplicates id same element then its length will be less that original list

def all_unique(item):
    return len(set(item)) == len(item)

OUT

>>> all_unique(['a', 'b', 'x', 'y'])
True
>>> all_unique(['a', 'a', 'x', 'y'])
False
>>> all_unique(['a', 'a', 'a', 'a'])
False
>>>
Community
  • 1
  • 1
Roushan
  • 4,074
  • 3
  • 21
  • 38
2

In your case you have written len(set(list)) != 1 which only works if all elements in list are same if you have two elements repeats for thousand of time then it will return length of set as two because it will have two unique elements.

So one way is that you check length of unique elements with original list if its same then function evaluates True otherwise False

def all_distinct(lis):
    return len(set(lis)) == len(lis)

set(lis) - creates set of element where all elements are unique (never repeated) len() - returns length of set or list or string passed as argument.

In [8]: all_distinct(['a', 'a'])
Out[8]: False

In [9]: all_distinct(['a', 'a', 'x'])
Out[9]: False

In [11]: all_distinct(['a', 'b', 'x'])
Out[11]: True

You can also approach in different way as below (keeping record of occurances)

def get_occurrence(lis):
    td = {}
    for i in lis:
        td[i] = td.setdefault(i, 0) + 1
    return td

def all_distinct(lis):
    dict_ = get_occurrence(lis)  # this will return dict containing key as element and value of it's occurrence 
    return all(i==1 for i in dict_.values())
Gahan
  • 4,075
  • 4
  • 24
  • 44
  • `set(lis)` does not sort at all - sets are unordered - see : https://docs.python.org/3.6/library/stdtypes.html#set-types-set-frozenset : `A set object is an unordered collection of distinct hashable objects. ` – Patrick Artner Apr 21 '18 at 09:19