No duplicates

Documentation

  1. Class set in the Python Standard Library
  2. Sets in the Python Tutorial

A set can not contain duplicates.

Note that the following set function does not give us any error message when we try to insert two copies of "Grover Cleveland" into the set.

"""
Remove extra copies, if any, from a list.
"""

import sys

listOfPresidents = [
    None,                     #Make George Washington #1.
    "George Washington",      # 1
    "John Adams",             # 2
    "Thomas Jefferson",       # 3
    "James Madison",          # 4
    "James Monroe",           # 5
    "John Quincy Adams",      # 6
    "Andrew Jackson",         # 7
    "Martin Van Buren",       # 8
    "William Henry Harrison", # 9
    "John Tyler",             #10
    "James K. Polk",          #11
    "Zachary Taylor",         #12
    "Millard Fillmore",       #13
    "Franklin Pierce",        #14
    "James Buchanan",         #15
    "Abraham Lincoln",        #16
    "Andrew Johnson",         #17
    "Ulysses S. Grant",       #18
    "Rutherford B. Hayes",    #19
    "James A. Garfield",      #20
    "Chester A. Arthur",      #21
    "Grover Cleveland",       #22 (non-consecutive)
    "Benjamin Harrison",      #23
    "Grover Cleveland",       #24 (non-consecutive)
    "William McKinley",       #25
    "Theodore Roosevelt",     #26
    "William Howard Taft",    #27
    "Woodrow Wilson",         #28
    "Warren G. Harding",      #29
    "Calvin Coolidge",        #30
    "Herbert Hoover",         #31
    "Franklin D. Roosevelt",  #32
    "Harry S. Truman",        #33
    "Dwight D. Eisenhower",   #34
    "John F. Kennedy",        #35
    "Lyndon B. Johnson",      #36
    "Richard Nixon",          #37
    "Gerald Ford",            #38
    "Jimmy Carter",           #39
    "Ronald Reagan",          #40
    "George H. W. Bush",      #41
    "Bill Clinton",           #42
    "George W. Bush",         #43
    "Barack Obama",           #44
    "Donald Trump"            #45
]

setOfPresidents = set(listOfPresidents[1:])

for i, president in enumerate(setOfPresidents, start = 1):
    print(f"{i:2} {president}")

sys.exit(0)
 1 John Tyler
 2 Franklin Pierce
 3 William McKinley
 4 Theodore Roosevelt
 5 John Quincy Adams
 6 Calvin Coolidge
 7 Grover Cleveland
 8 Herbert Hoover
 9 John Adams
10 William Henry Harrison
11 James Monroe
12 Chester A. Arthur
13 Millard Fillmore
14 Gerald Ford
15 Zachary Taylor
16 James Buchanan
17 Martin Van Buren
18 Dwight D. Eisenhower
19 Abraham Lincoln
20 Bill Clinton
21 John F. Kennedy
22 Woodrow Wilson
23 George Washington
24 Ronald Reagan
25 Harry S. Truman
26 James A. Garfield
27 Jimmy Carter
28 George H. W. Bush
29 William Howard Taft
30 Donald Trump
31 Lyndon B. Johnson
32 Warren G. Harding
33 Barack Obama
34 Richard Nixon
35 Andrew Johnson
36 Franklin D. Roosevelt
37 James Madison
38 James K. Polk
39 Andrew Jackson
40 George W. Bush
41 Ulysses S. Grant
42 Thomas Jefferson
43 Rutherford B. Hayes
44 Benjamin Harrison

Things to try

  1. To preserve the original order while removing duplicates, make a dictionary (in Python version ≥ 3.7) or a collections.OrderedDict.
    dictOfPresidents = {president: None for president in listOfPresidents[1:]}
    
    for i, president in enumerate(dictOfPresidents, start = 1):
        print(f"{i:2} {president}")
    
     1 George Washington
     2 John Adams
     3 Thomas Jefferson
     4 James Madison
     5 James Monroe
     6 John Quincy Adams
     7 Andrew Jackson
     8 Martin Van Buren
     9 William Henry Harrison
    10 John Tyler
    11 James K. Polk
    12 Zachary Taylor
    13 Millard Fillmore
    14 Franklin Pierce
    15 James Buchanan
    16 Abraham Lincoln
    17 Andrew Johnson
    18 Ulysses S. Grant
    19 Rutherford B. Hayes
    20 James A. Garfield
    21 Chester A. Arthur
    22 Grover Cleveland
    23 Benjamin Harrison
    24 William McKinley
    25 Theodore Roosevelt
    26 William Howard Taft
    27 Woodrow Wilson
    28 Warren G. Harding
    29 Calvin Coolidge
    30 Herbert Hoover
    31 Franklin D. Roosevelt
    32 Harry S. Truman
    33 Dwight D. Eisenhower
    34 John F. Kennedy
    35 Lyndon B. Johnson
    36 Richard Nixon
    37 Gerald Ford
    38 Jimmy Carter
    39 Ronald Reagan
    40 George H. W. Bush
    41 Bill Clinton
    42 George W. Bush
    43 Barack Obama
    44 Donald Trump
    
  2. Do the work in pandas.
    """
    Remove extra copies, if any, from a list.
    """
    
    import sys
    import pandas as pd
    import numpy as np
    
    listOfPresidents = [
        None,
        "George Washington",      # 1
        "John Adams",             # 2
        "Thomas Jefferson",       # 3
        "James Madison",          # 4
        "James Monroe",           # 5
        "John Quincy Adams",      # 6
        "Andrew Jackson",         # 7
        "Martin Van Buren",       # 8
        "William Henry Harrison", # 9
        "John Tyler",             #10
        "James K. Polk",          #11
        "Zachary Taylor",         #12
        "Millard Fillmore",       #13
        "Franklin Pierce",        #14
        "James Buchanan",         #15
        "Abraham Lincoln",        #16
        "Andrew Johnson",         #17
        "Ulysses S. Grant",       #18
        "Rutherford B. Hayes",    #19
        "James A. Garfield",      #20
        "Chester A. Arthur",      #21
        "Grover Cleveland",       #22 (non-consecutive)
        "Benjamin Harrison",      #23
        "Grover Cleveland",       #24 (non-consecutive)
        "William McKinley",       #25
        "Theodore Roosevelt",     #26
        "William Howard Taft",    #27
        "Woodrow Wilson",         #28
        "Warren G. Harding",      #29
        "Calvin Coolidge",        #30
        "Herbert Hoover",         #31
        "Franklin D. Roosevelt",  #32
        "Harry S. Truman",        #33
        "Dwight D. Eisenhower",   #34
        "John F. Kennedy",        #35
        "Lyndon B. Johnson",      #36
        "Richard Nixon",          #37
        "Gerald Ford",            #38
        "Jimmy Carter",           #39
        "Ronald Reagan",          #40
        "George H. W. Bush",      #41
        "Bill Clinton",           #42
        "George W. Bush",         #43
        "Barack Obama",           #44
        "Donald Trump"            #45
    ]
    
    series = pd.Series(listOfPresidents[1:])   #series is a pandas Series
    series.drop_duplicates(inplace = True)
    series.index = np.arange(1, series.size + 1) #or series.index = range(1, len(series) + 1)
    
    s = series.to_string(index = True, length = True)
    print(s)
    sys.exit(0)
    
    1          George Washington
    2                 John Adams
    3           Thomas Jefferson
    4              James Madison
    5               James Monroe
    6          John Quincy Adams
    7             Andrew Jackson
    8           Martin Van Buren
    9     William Henry Harrison
    10                John Tyler
    11             James K. Polk
    12            Zachary Taylor
    13          Millard Fillmore
    14           Franklin Pierce
    15            James Buchanan
    16           Abraham Lincoln
    17            Andrew Johnson
    18          Ulysses S. Grant
    19       Rutherford B. Hayes
    20         James A. Garfield
    21         Chester A. Arthur
    22          Grover Cleveland
    23         Benjamin Harrison
    24          William McKinley
    25        Theodore Roosevelt
    26       William Howard Taft
    27            Woodrow Wilson
    28         Warren G. Harding
    29           Calvin Coolidge
    30            Herbert Hoover
    31     Franklin D. Roosevelt
    32           Harry S. Truman
    33      Dwight D. Eisenhower
    34           John F. Kennedy
    35         Lyndon B. Johnson
    36             Richard Nixon
    37               Gerald Ford
    38              Jimmy Carter
    39             Ronald Reagan
    40         George H. W. Bush
    41              Bill Clinton
    42            George W. Bush
    43              Barack Obama
    44              Donald Trump
    Length: 44
    

    To left justify the numbers and right justify the strings,

    for i, name in series.items():   #series is a pandas Series
        print(f"{i:2} {name}")
    
     1 George Washington
     2 John Adams
     3 Thomas Jefferson
     4 James Madison
     5 James Monroe
     6 John Quincy Adams
     7 Andrew Jackson
     8 Martin Van Buren
     9 William Henry Harrison
    10 John Tyler
    etc.