List Basis
*args
scores = [72, 73, 33]
average = sum(scores) / len(scores)
print(f"Average: {average}")
The f
before the double quotes indicates that this is a format string, which will allow us to use curly braces, {}
, to include variables that should be substituted, or interpolated. Python f-string
It is possible to nest lists (create lists containing other lists), for example:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
More on Lists
list.append(x)
- Add an item to the end of the list. Equivalent to
a[len(a):] = [x]
.
- Add an item to the end of the list. Equivalent to
list.extend(iterable)
- Extend the list by appending all the items from the iterable. Equivalent to
a[len(a):] = iterable
.
- Extend the list by appending all the items from the iterable. Equivalent to
list.insert(i, x)
- Insert an item at a given position. The first argument is the index of the element before which to insert, so
a.insert(0, x)
inserts at the front of the list, anda.insert(len(a), x)
is equivalent toa.append(x)
.
- Insert an item at a given position. The first argument is the index of the element before which to insert, so
list.remove(x)
- Remove the first item from the list whose value is equal to x. It raises a
ValueError
if there is no such item.
- Remove the first item from the list whose value is equal to x. It raises a
list.pop([i])
- Remove the item at the given position in the list, and return it. If no index is specified,
a.pop()
removes and returns the last item in the list. - The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.
- Remove the item at the given position in the list, and return it. If no index is specified,
list.clear()
- Remove all items from the list. Equivalent to
del a[:]
.
- Remove all items from the list. Equivalent to
list.index(x[, start[, end]])
- Return zero-based index in the list of the first item whose value is equal to x. Raises a
ValueError
if there is no such item. - The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
- Return zero-based index in the list of the first item whose value is equal to x. Raises a
list.count(x)
- Return the number of times x appears in the list.
list.sort(*, key=None, reverse=False)
- Sort the items of the list in place (the arguments can be used for sort customization, see
sorted()
for their explanation).
- Sort the items of the list in place (the arguments can be used for sort customization, see
list.reverse()
- Reverse the elements of the list in place.
list.copy()
- Return a shallow copy of the list. Equivalent to
a[:]
.
- Return a shallow copy of the list. Equivalent to
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.count('tangerine')
0
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4) # Find next banana starting a position 4
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'