# Noemi Cabrera
# 27 October 2021
# In this lesson, I learned how to modify and insert items into a list. To modify or
# overwrite an existing index in a list, you use = with the item you want to add after
# you have sliced and indicated the index you want to replace.To insert an item you use
# the .insert() method in which you indicate at which index you want add your item
# followed by the item you want to add.
# I didn't have any difficulties in this lesson.
# [ In this code, an item in the variable party list is replaced. You do a slice with the
# index you want to replace and then you set that equal to what you want to replace it
# with. In this case, Alton is replaced with Collete. ] review and run example
# the list before Insert
party_list = ["Joana", "Alton", "Tobias"]
print("party_list before: ", party_list)
# the list after Insert
party_list[1] = "Colette"
print("party_list after: ", party_list)
# [ In this code, the string at index 1 from party_list is added another string. "Alton" equals to itsefl
# plus "Derosa", it's not replaced. ] review and run example
party_list = ["Joana", "Alton", "Tobias"]
print("before:",party_list)
# modify index value
party_list[1] = party_list[1] + " Derosa"
print("\nafter:", party_list)
# IndexError Example
# [ The variable party_list has only indexes 0, 1, and 2. Therefore, when trying to acess and change
# index 3 and error appears because this index doesn't exist in the variable. ] review and run example
# which results in an IndexError
# if result is NameError run cell above before running this cell
# IndexError trying to append to end of list
party_list[3] = "Alton"
print(party_list)
# [ In this code, the data type of index 3 in the single_digits list is shown by using the
# type() method. Then, index 3 is replaced with integer 3 and its type is shown too. ]
# review and run example changes the data type of an element
# replace a string with a number (int)
single_digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
print("single_digits: ", single_digits)
print("single_digits[3]: ", single_digits[3], type(single_digits[3]),"\n")
# replace string with an int
single_digits[3] = 3
print("single_digits: ", single_digits)
print("single_digits[3]: ", single_digits[3], type(single_digits[3]))
# [ In this code, the three_num list is defined and its item at index 0 changes depending
# on its value. With the if/else statement, we determine what the item at index 0 is
# replaced with. If integer is less than 5, it's replaced with the string "small",
# but if greater than 5, integer is replaced with string "large". ] complete "replace
# items in a list" task
three_num = [5, 9, 3]
print(three_num)
if three_num[0]<5:
three_num[0] = "small"
else:
three_num[0] = "large"
print(three_num)
# [ The str_replace() function is defined. It takes 2 arguments: a list and an index.
# It replaces the index on the list, which are given as an argument, with a string
# depending on its value. The final list is printed.] create challenge function
def str_replace(int_list,index):
if int_list[index]<5:
int_list[index] = "small"
else:
int_list[index] = "large"
print(int_list)
str_replace([2,3,8],2)
# [ In this code, 2 items of the variable three_words are modified. The string "Happy" at
# index 0 is made uppercase and the string "Rainbow" is swapcased. ]
# complete coding task described above
three_words = ["Happy","Shoe","Rainbow"]
print(three_words)
three_words[0] = three_words[0].upper()
three_words[2] = three_words[2].swapcase()
print(three_words)
# [ The insert() method is similar to the append() method because it adds something to a
# list, it doesn't replace. However, with this method you can specify at which index you
# want to add the item. In this case, the string "Colette"is added at index 1 and the
# other items have to change indexes. ] review and run example
# the list before Insert
party_list = ["Joana", "Alton", "Tobias"]
print("party_list before: ", party_list)
print("index 1 is", party_list[1], "\nindex 2 is", party_list[2], "\n")
# the list after Insert
party_list.insert(1,"Colette")
print("party_list after: ", party_list)
print("index 1 is", party_list[1], "\nindex 2 is", party_list[2], "\nindex 3 is", party_list[3])
# [ In this case, the insert() method adds an item that is get from user input. A variable
# contains the input function and then it's placed in the insert() methd after you
# indicated the index. ] insert a name from user input into the party_list in the second
# position (index 1)
party_list = ["Joana", "Alton", "Tobias"]
partyname = input("Enter your name to add you to our party list: ")
party_list.insert(1,partyname)
# [ The final list is printed ] print the updated list
print(party_list)
# [ The error in this code was that the variable tree_list had a string as its value, not a list with
# square brackets. To fix this, I added [] to the string "oak". ] Fix the Error
tree_list = ["oak"]
print("tree_list before =", tree_list)
tree_list.insert(1,"pine")
print("tree_list after =", tree_list)