Lists and Tuples Part 1 - ch07 - Part1
Lists and Tuples Part 1 - ch07 - Part1
Chapter 7
Lists and Tuples
In the following loop, list elements are accessed using index numbers.
number_list = [10, 20, 30]
for i in range(0, len(number_list)):
print(number_list[i])
It is now possible to modify the list elements directly as shown in the loop
example above. E.g. list[i]*=2
print(number_list[5:2])
print(number_list[5:1:-1])
Method Description
append(item) Adds item to the end of the list.
index(item) Returns the index of the first element whose value is equal to item.
A ValueError exception is raised if item is not found in the list.
insert(index, item) Inserts item into the list at the specified index. When an item is inserted into a
list, the list is expanded in size to accommodate the new item. The item that
was previously at the specified index, and all the items after it, are shifted by
one position toward the end of the list. No exceptions will occur if you specify an
invalid index. If you specify an index beyond the end of the list, the item will be
added to the end of the list. If you use a negative index that specifies an invalid
position, the item will be inserted at the beginning of the list.
sort() Sorts the items in the list so they appear in ascending order (from the lowest
value to the highest value).
remove(item) Removes the first occurrence of item from the list. A ValueError exception
is raised if item is not found in the list.
reverse() Reverses the order of the items in the list.