CSE 1000 Lecture 5: Lists
Document Summary
List a sequence of comma-separated values between square brackets. Like strings, lists can be indexed or sliced: squares [0] Squares [-3:] (start at -3 and go to the end of the line, aka last 3 numbers in line) The items in a list need not be of the same type: list = ["physics", "chemistry", 1997, 2000] Updating lists - changing value of list. Deleting list elements: del list [2] List buildup: append always goes to end of the list. Cannot work with numbers, numbers have to be in a list: list. insert(0, "x") insert method inserts element into list before the index. Index method returns the lowest index in list that element appears: list. index("b") Remove method removes element from list (removes first appearance of element in list if it is repeated: list. remove("b", list. ["x", "a", "b", "c": del uses an index number, so if you want to delete a second occurance use del instead of remove.