0

Let's say I have a list

List = ['Antonia', 'Sara', 'Nick', 'Deppy', 'Antonia', 'Deppy', 'Antonia']

And I want to count all of them

Nick: 1

Sara: 1

Deppy: 2

Antonia: 3

Is there a way to make a new list which they are ordered like this:

New_List = ['Antonia', 'Deppy', 'Sara', 'Nick']

?

1 Answers1

0

There are many ways to achieve your goal. Here is a simple way to do:

the_list = ['Antonia', 'Sara', 'Nick', 'Deppy', 'Antonia', 'Deppy', 'Antonia']

# count unique item in the list 'the_list'
for item in set(the_list):
    print(item, ':', the_list.count(item))

# an ordered new list containing unique item of 'the_list'
new_list = sorted([item for item in set(the_list)])
print(new_list)

Outputs:

Nick : 1
Antonia : 3
Sara : 1
Deppy : 2

['Antonia', 'Deppy', 'Nick', 'Sara']
codrelphi
  • 1,075
  • 1
  • 7
  • 13
  • The first part is correct,but the new_list is not very correct... – JimChr - R4GN4R Jan 14 '20 at 17:49
  • `Nick`comes before `Sara` because the list is *alphabetically* sorted. You make a mistake in your post. Go check it. – codrelphi Jan 14 '20 at 17:51
  • Yeah,I meatn he who has the biggest number will be first,then the next one etc. – JimChr - R4GN4R Jan 14 '20 at 17:57
  • I don't understand your point. Please, make your point more clear! – codrelphi Jan 14 '20 at 18:13
  • Let's say I have Antonia 5 times and Nikos 3 times. /// So I want the first one to be he who is written most of the times. So Antonia > Nikos. As a result the list has first Antonia and then Nikos. If Nikos was written 6 times, then Nikos > Antonia. As a result first in the new_list would be Nikos and then Antonia. – JimChr - R4GN4R Jan 14 '20 at 19:42