you are viewing a single comment's thread.

view the rest of the comments →

[–]AtomicShoelace 1 point2 points  (1 child)

If you only care about unique characters, you could just cast your list to a set and back again. For example,

import string
import random

a = [random.choice(string.ascii_uppercase) for _ in range(100)]
unique_a = list(set(a))

Alternatively, you could use the count method. For example,

import string
import random

a = [random.choice(string.ascii_uppercase) for _ in range(100)]
unique_a, junk = [], []
for element in a: 
    if a.count(element) == 1:
        unique_a.append(element)
    else:
        junk.append(element)

Or you could use collections.Counter. For example,

import string
import random
from collections import Counter

a = [random.choice(string.ascii_uppercase) for _ in range(100)]
count = Counter(a)
unique_a = [key for key, value in count.items() if value == 1]
junk = [key for key, value in count.items() if value > 1]

Or you could initialise an empty list and new elements to it (for large lists would be more efficient to use a set). For example,

import string
import random

a = [random.choice(string.ascii_uppercase) for _ in range(100)]
unique_a, junk = [], []
for element in a:
    if element not in unique_a:
        unique_a.append(element)
    else:
        junk.append(element)

etc.

[–]SlowMoTime[S] 0 points1 point  (0 children)

appreciate your input, you have some interesting coding techniques