Other list functionality

Splitting a string

We split strings the hard way before because we hadn’t learnt about lists, but now we can use the easy method known as the split() function.

birthday = input("Please enter your date of birth as dd/mm/yyyy : ")
parts = birthday.split("/")
print("Your day of birth is ", parts[0])
print("Your month of birth is ", parts[1])
print("Your year of birth is ", parts[2])

Joining a list together

Just as we can split a string into a list, we can join it back together again too.

addressList = ["Chemin de la Grangette 2","1052 Le Mont-sur-Lausanne","Switzerland"]
newAddress = ", ".join(addressList)
print(newAddress)

Appending an item to a list

beatlesMembers = ["John", "Paul", "George"]
beatlesMembers.append("Ringo")

Deleting an item from a list

beatlesMembers = ["John", "Paul", "George", "Ringo"]
beatlesMembers.remove("Ringo")

Delete an item using it’s index number.

beatlesMembers = ["John", "Paul", "George", "Ringo"]
beatlesMembers.pop(1)   # will remove the 2nd value (start counting from zero)
print(beatlesMembers)   # ["John", "George", "Ringo"]