© 2004-21

Adding data to a list

There are a number of different ways to add data to a list including creating a list from a text strings as shoiwn in the examples below
#Author: A Knifton
#Date: 19 Feb 2018
#Purpose: modifying  list with apend and insert

print ("{} Adding Data to a List {} \n".format("-"*6,"-"*6))

myList = ['Orange','Grapefruit','Lemon','Mango']
print ('Initial List: {}\n'.format(myList))

print("Append - adds an item at the END of the list ")
print ('Item {} appended: {}\n'.format("Pear",myList))

print("Insert - adds an item at a specific place identified by the index number")
# Note1 The existing data will be overwritten
# Note2 the index number must exist otherwise the item will be inserted at the end
position = 2

print ('Item {} inserted {} into the list:\nNOTE: this is how you insert data \n'
       'at the beginning of the list'.format("Apple",position,myList))

phraseMinecraft = "When a creeper blows up in a forest and no mobs are " \
                  "around to hear it does it make a sound?"
print("\n\nCreating a list from a text string:\n{}\n\n".format(phraseMinecraft))

#New see what the list looks like
listMinecraft = phraseMinecraft.split(" ")
print ('listMinecraft: {} \n'.format(listMinecraft))

# making a string from a list
phraseNew = " ".join(listMinecraft)