0

i want to remove repeating elements from scores and store them in ranks.

    scores = [1,2,3,3,4,5,5,5]
    ranks = []
    for x in scores:
        if x not in ranks:
            ranks.append(x)
  • 4
    ```set(scores)``` – sushanth Jul 20 '20 at 07:08
  • 1
    `ranks = list(set(scores))` – Abhishek Singh Jul 20 '20 at 07:20
  • i want the code using list comprehension – Rahul Kulkarni Jul 20 '20 at 07:43
  • you can't do this with `list comprehension` because it checks value in `ranks` - `if x not in ranks:` - which not exists yet. `ranks` will exists after running `list comprehension` – furas Jul 20 '20 at 11:27
  • BTW: eventually you could use `dict comprehension` because `dict` can't repeate keys - `list({key:None for key in scores})` - or write `set()` like list comprehension - `list(set(key for key in scores))` but all this make no sense if you can use `list(set(scores))` – furas Jul 20 '20 at 11:37
  • the only idea `[key for index, key in enumerate(scores) if index == scores.index(key)]` which gets only first occurrence of item (which gives `.index(key)`) - but it is overcomplicated if you can use `set(scores)` – furas Jul 20 '20 at 11:38

0 Answers0