3.2 List basics¶
Creating a list¶
- A container is a construct used to group related values together and contains references to other objects instead of data.
- A list is a container created by surrounding a sequence of variables or literals with brackets [ ].
- Ex: my_list = [10, 'abc']
- A list item is called an element
- A list is also a sequence, meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0
- lists can be empty
-
lists are mutable, which means they can be altered:
Add and remove elements from list¶
- A method instructs an object to perform some action, and is executed by specifying the method name following a "." symbol and an object.
- list.append(value): Adds value to the end of list.
- Ex: my_list.append('abc')
- list.pop(i): Removes the element at index i from list.
- Ex: my_list.pop(1)
- list.remove(v): Removes the first element whose value is v.
- Ex: my_list.remove('abc')
- Sequence-type methods and functions
Example: