'Getting the Middle Name in a list

I want to create a function that removes all of the middle name and leaves the First and Last name when given the format First, Middle, Last.

So far, I have

def clearmidname(listnames):
    
    for name in listnames:
        name = name.split()
        del name[1]
        new = " ".join(name)

    return new

For example,

listnames = ["Orpah Gail Winfrey", "Montero Lamar Hill", "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso"]

print(clearmidname(listnames))

would give me

["Orpah Winfrey", "Montero Hill", "Pablo Picasso"]


Solution 1:[1]

def clearmidname(listnames):
    retlist=[]
    for name in listnames:
        name = name.split(" ")
        retlist.append(name[0] + " " + name[-1])

   return retlist

Solution 2:[2]

This is pretty simple, you can jsut .split (perhaps using re.split if there are complicated word boudaries) and just use extended iterable to seperate the first and last words from the middle:

>>> names = ["Orpah Gail Winfrey", "Montero Lamar Hill", "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso"]
>>> for name in names:
...     first, *middle, last = name.split()
...     print(f"{first} {last}")
...
Orpah Winfrey
Montero Hill
Pablo Picasso

So to create the list, just:

new = []
for name in names:
    first, *_, last = name.split()
    new.append(f"{first} {last}")

You can also use a list comprehension:

[f"{first} {last}" for name in names for first, *_, last in [name.split()]]

Solution 3:[3]

Your problem is a good candidate for a regex replacement:

# -*- coding: utf-8 -*-
import re

listnames = ["Orpah Gail Winfrey", "Montero Lamar Hill", "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso"]
output = [re.sub(r' \S+(?= )', '', name) for name in listnames]
print(output)  # ['Orpah Winfrey', 'Montero Hill', 'Pablo Picasso']

Solution 4:[4]

Using similar logic with your code, with some modifications:

  1. using a list to store your modified names instead of keep updating the same variable "new" in each iteration
  2. Instead of removing the "middle" element in you split list, take the first and last element to avoid multiple "middle name"

p.s. I added an "Orpah Gail G Winfrey" into the "listnames" to illustrate my point 2.

def clearmidname(listnames):
    new = []
    for name in listnames:
        name = name.split()
        new.append(" ".join([name[0], name[-1]]))
    return new
    
listnames = ["Orpah Gail G Winfrey", "Orpah Gail Winfrey", "Montero Lamar Hill", "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso"]

print(clearmidname(listnames))

Result:

['Orpah Winfrey', 'Orpah Winfrey', 'Montero Hill', 'Pablo Picasso']

Solution 5:[5]

You can do this in one Pythonic line:

first_last_names_list = [name.split()[0]+" "+name.split()[-1] for name in listnames]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 obayhan
Solution 2 juanpa.arrivillaga
Solution 3 Tim Biegeleisen
Solution 4 ytung-dev
Solution 5 Nicolae