
Style: it's a nice pattern to name the list variable with the letter "s" like "urls". It's very easy to "foreach" loop over all the elements in a list, seeing each element once. Omitting both start and end indexes yields a copy of the whole list - lst Foreach loop - for elem in list The original list is not modified, this creates a new list populated with elements from the original. Slices work to pull out parts of list just as with strings. (mentioned for completeness, I don't think we will ever need this function in CS106A.) You could use this to loop over a list and also modify it - loop over a copy, modify the original. Append() is simpler since it just goes on the end without any shifting and you don't have to think about index numbers. Lst.insert(index, x) - insert the element x so it is at the given index, shifting elements towards the end of the list as needed. Min(lst) max(lst) - Return the smallest or largest element in lst. > 'x' in lst # Therefore, check before calling. In other words, there is nothing as simple as str.find() for lists which IMHO seems Therefore check with in first, and only if x is in there call index(). Raises an error if x is not in there - this is rather inconvenient. Lst.index(x) - Look for first instance of x in lst and return its index. The + operation is an alternative to extend(), combining lists to make a bigger list (very analogous to + with strings) > # all elements of x are added at end, so lst is > # x is added as an *element* so lst is ] Lst.extend(lst2) - add all the elements of lst2 on to the end of lst.

ValueError: list.remove(x): x not in list Note that pop() uses index numbers, but remove() uses the value, e.g. It's an error to remove() an elem not in the list - could use in to check first. Lst.remove( elem) - search the list for the first instance of elem and remove it. Raises an error if the index is not valid. lst.pop(0) removes the element at index 0. Lst.pop( index) - alternate version with the index to remove is given, e.g. Mnemonic: the exact opposite of append(). Lst.pop() - remove the element from the end of the list and return it, decreasing the length of the list by 1. Lst.append(x) - add x to the end of lst, increasing its length by 1. X in lst - boolean test if x is in lst (just like for string) Lst - access individual elements with square bracketsįor x in lst: - loop over contents, do not modify lst during loop
#MAKE A LIST PYTHON SERIES#
As a convenience, it's allowable to have an extra comma at the end of the series of elements like this: Here is what that list looks like in memory The list is written within square brackets, and the elements are separated by commas.

#MAKE A LIST PYTHON CODE#
Here is the code to create a list of the three strings 'a' 'b' and 'c'. As elements are added and removed, Python maintains the elements as contiguous block alwayx indexed by the numbers 0.length-1 inclusive. Using the standard indexing scheme, the first element is at index 0, the next at index 1, and so on up to index length-1.

Unlike strings, lists are "mutable" - they can be changed. The things inside a list are generically called "elements".

A list contains series of any data type: strings, ints, other lists.
