i have this list:
['Boston Americans', 'New York Giants', 'Chicago White Sox', 'Chicago Cubs', 'Chicago Cubs', 'Pittsburgh Pirates', 'Philadelphia Athletics', 'Philadelphia Athletics', 'Boston Red Sox', 'Philadelphia Athletics', 'Boston Braves', 'Boston Red Sox', 'Boston Red Sox', 'Chicago White Sox', 'Boston Red Sox', 'Cincinnati Reds', 'Cleveland Indians', 'New York Giants', 'New York Giants', 'New York Yankees', 'Washington Senators', 'Pittsburgh Pirates', 'St. Louis Cardinals', 'New York Yankees', 'New York Yankees', 'Philadelphia Athletics', 'Philadelphia Athletics', 'St. Louis Cardinals', 'New York Yankees', 'New York Giants', 'St. Louis Cardinals', 'Detroit Tigers', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'Cincinnati Reds', 'New York Yankees', 'St. Louis Cardinals', 'New York Yankees', 'St. Louis Cardinals', 'Detroit Tigers', 'St. Louis Cardinals', 'New York Yankees', 'Cleveland Indians', 'New York Yankees', 'New York Yankees']
How can i remove duplicate from this list without using the count, append or the set method or imports?
Or what i really want is: how can i turn that list to print out like this:
Boston Americans 5
New York Giants 2
team_name number_of_duplicates
team_name number_of_duplicates
team_name number_of_duplicates
Result:
I used this code:
And it produced this result: output of the code
Hope it helps.
To count how many of each entry there are in the list you can use the
Counter
class in thecollections
module:c
is then aCounter
object which holds the number of occurrences in the list for each distinct entry/key. AsCounter
is derived fromdict
, you can access it like any other dictionary.without using
list.count
at all:You did not say whether you can use a dict or not so:
You could make new list, e.g.
Gives: