You can accumulate lists, just like numbers and strings! This is useful if you have two lists that you want to combine into a new list:
>>> L = [] >>> newlist = ["A","B","C"] >>> L = L + newlist >>> print(L) ['A', 'B', 'C'] >>> newlist = ["D","E","F"] >>> L = L + newlist >>> print(L) ['A', 'B', 'C', 'D', 'E', 'F']
If you want, you can also use the extend method for lists which mutates the list and extends it by adding the other list to the end. Here's an example:
>>> L = [] >>> newlist = ["A","B","C"] >>> L.extend(newlist) >>> print(L) ['A', 'B', 'C'] >>> newlist = ["D","E","F"] >>> L.extend(newlist) >>> print(L) ['A', 'B', 'C', 'D', 'E', 'F']
There's also an append() method for lists, to add just one item to a list. Here's an example:
>>> L = [] >>> L.append("A") >>> print(L) ['A'] >>> L.append("B") >>> print(L) ['A', 'B'] >>> L.append("hello") >>> print(L) ['A', 'B', 'hello']