Confused about the type of parameter that goes into this method

  list, python, python-3.x, string, syntax

I’m trying to understand how this code works, we have:

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson',
          'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):

    return person.split()[0] + ' ' + person.split()[-1]

So we are given a list, and this method is supposed to basically delete everything in the middle between "Dr." and the last name. As far as I know, the split() function cannot be used for lists, but for strings. so person must be a string. However, we also add [0] and [-1] to person, which means we should be getting the first and last character of "person" but instead, we get first word and last word. I cannot make sense of this code! May you please help me understand?

Any help is greatly appreciated, thank you 🙂

Source: Python-3x Questions

LEAVE A COMMENT