You have already learnt about lists in a previous tutorial. In this tutorial, we're going to dive deeper into Python lists.
Python has a great built-in list type named list. List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.
type=codeblock|id=py3_list_review|autocreate=python3colors = ['red', 'blue', 'green']print(colors) ## redprint(colors) ## greenprint(len(colors)) ## 3
Assignment with an = on lists does not make a copy. Instead, assignment makes the two variables point to the one list in memory.
type=codeblock|id=py3_list_reference|autocreate=python3colors = ['red', 'blue', 'green']b = colors ## Does NOT copy the listcolors = 'yellow'print(colors) # ['red', 'yellow', 'green']print(b) # ['red', 'yellow', 'green']
The "empty list" is just an empty pair of brackets [ ]. The + works to append two lists, so [1, 2] + [3, 4] yields [1, 2, 3, 4] (this is just like + with strings).