Python – Get Last Element after str.split()

I use pandas and I have data and the data look like this

FirstName LastName StudentID
FirstName2 LastName2 StudentID2

Then I split it based on ‘space’ using str.split()

So the data will look like this in DataFrame

[[FirstName, LastName, StudentID],
[FirstName2, LastName2, StudentID2]]

How to take the StudentID for every students only and save it in new column?

Answer

Use a list comprehension to take the last element of each of the split strings:

ids = [val[-1] for val in your_string.split()]