Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to count unique values in a list with Python

4 Answers

0 votes
from collections import Counter

lst = ['python', 'c', 'c++', 'c', 'c++', "c++", "swift"]


print(Counter(lst).keys())

print(Counter(lst).values())

print(Counter(lst))





'''
run:

dict_keys(['python', 'c', 'c++', 'swift'])
dict_values([1, 2, 3, 1])
Counter({'c++': 3, 'c': 2, 'python': 1, 'swift': 1})

'''

 



answered Apr 19, 2021 by avibootz
0 votes
from collections import Counter

lst = ['python', 'c', 'c++', 'c', 'c++', "c++", "swift"]


dict = Counter(lst)


for key, value in dict.items():
    print(key, ":", value)
  





'''
run:

python : 1
c : 2
c++ : 3
swift : 1

'''

 



answered Apr 19, 2021 by avibootz
0 votes
lst = ['python', 'c', 'c++', 'c', 'c++', "c++", "swift"]

print(len(set(lst)))
  





'''
run:

4

'''

 



answered Apr 19, 2021 by avibootz
0 votes
import numpy as np

lst = ['python', 'c', 'c++', 'c', 'c++', "c++", "swift"]

np.unique(lst)

print(len(np.unique(lst)))
  




'''
run:

4

'''

 



answered Apr 19, 2021 by avibootz
...