0

This is my list:

premier_league = [
                 ['Manchester City', '1', 'Aguero'],
                 ['Manchester City', '1', 'Mahrez'],
                 ['Manchester City', '1', 'Sterling'],
                 ['Liverpool', '2', 'Mane'],
                 ['Liverpool', '2', 'Salah'],
                 ['Liverpool', '2', 'Jota'],
                 ['Chelsea', '3', 'Ziyech'],
                 ['Chelsea', '3', 'Werner'],
                 ['Chelsea', '3', 'Abraham']
                 ]

I'm trying to get a new list which contains only the name of the football clubs once. This is my code:

clubs = []
for club in premier_league:
    clubs.append(club[0])
    
print(clubs) 

This is my output:

 ['Manchester City', 'Manchester City', 
'Manchester City', 'Liverpool', 
'Liverpool', 'Liverpool', 
'Chelsea', 'Chelsea', 'Chelsea'] 

This is my desired output

['Manchester City', 'Liverpool', 'Chelsea']
quamrana
  • 37,849
  • 12
  • 53
  • 71
TangerCity
  • 775
  • 2
  • 7
  • 13

3 Answers3

1

You could simply get unique items by:

premier_league = [
                 ['Manchester City', '1', 'Aguero'],
                 ['Manchester City', '1', 'Mahrez'],
                 ['Manchester City', '1', 'Sterling'],
                 ['Liverpool', '2', 'Mane'],
                 ['Liverpool', '2', 'Salah'],
                 ['Liverpool', '2', 'Jota'],
                 ['Chelsea', '3', 'Ziyech'],
                 ['Chelsea', '3', 'Werner'],
                 ['Chelsea', '3', 'Abraham']
                 ]

clubs = []
for club in premier_league:
    clubs.append(club[0])
    
uniq_clubs = list(set(clubs))

print(uniq_clubs) 

output:

['Chelsea', 'Liverpool', 'Manchester City']
quamrana
  • 37,849
  • 12
  • 53
  • 71
billz
  • 44,644
  • 9
  • 83
  • 100
1

If you wanted a longer way of doing this, try this:

premier_leauge = [
                 ['Manchester City', '1', 'Aguero'],
                 ['Manchester City', '1', 'Mahrez'],
                 ['Manchester City', '1', 'Sterling'],
                 ['Liverpool', '2', 'Mane'],
                 ['Liverpool', '2', 'Salah'],
                 ['Liverpool', '2', 'Jota'],
                 ['Chelsea', '3', 'Ziyech'],
                 ['Chelsea', '3', 'Werner'],
                 ['Chelsea', '3', 'Abraham']
                 ]

clubs = []

for club in premier_leauge:
    clubs.append(club[0])

clubs = list(dict.fromkeys(clubs))

print(clubs)
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
  • This only produces the same order in Python 3.6+. For older Pythons, the result is unordered. – dawg Jan 05 '21 at 14:49
0

Given:

premier_league = [
                 ['Manchester City', '1', 'Aguero'],
                 ['Manchester City', '1', 'Mahrez'],
                 ['Manchester City', '1', 'Sterling'],
                 ['Liverpool', '2', 'Mane'],
                 ['Liverpool', '2', 'Salah'],
                 ['Liverpool', '2', 'Jota'],
                 ['Chelsea', '3', 'Ziyech'],
                 ['Chelsea', '3', 'Werner'],
                 ['Chelsea', '3', 'Abraham']
                 ]

If you do not care about order, you can just use a set:

>>> set(set(sl[0] for sl in premier_league)
{'Liverpool', 'Chelsea', 'Manchester City'}

Which can be turned into a list where the order is likely different than your original list:

>>> list(set(sl[0] for sl in premier_league))
['Chelsea', 'Manchester City', 'Liverpool'] # note order likely has changed...

If you do care about order there are several things you can do:

Python 3.6+:

 >>> list({}.fromkeys(sl[0] for sl in premier_league))
 ['Manchester City', 'Liverpool', 'Chelsea']

Python older than 3.6, several ways:

1 Use an OrderedDict fromkeys method:

from collections import OrderedDict 
list(OrderedDict.fromkeys(sl[0] for sl in premier_league))
 

2 The slightly hacky set.add method in a list comprehension:

seen=set()
[e[0] for e in premier_league if not (e[0] in seen or seen.add(e[0]))]

3 Use groupby:

from itertools import groupby
[k for k,v in groupby(premier_league, key=lambda sl: sl[0])]

All three of those print:

['Manchester City', 'Liverpool', 'Chelsea']
dawg
  • 98,345
  • 23
  • 131
  • 206