Lists & Tuples: CSBP119 Algorithms & Problem Solving Cit, Uaeu
Lists & Tuples: CSBP119 Algorithms & Problem Solving Cit, Uaeu
Chapter 7
Lists & Tuples
• Sequences
• Introduction to Lists
• List Slicing
• Finding Items in Lists with the in Operator
• List Methods and Useful Built-in Functions
• Copying Lists
• Processing Lists
• List Comprehensions
• Two-Dimensional Lists
• Tuples
• Repetition
operator: makes
multiple copies of a list
and joins them together
• The * symbol is a
repetition operator
when applied to a
sequence and an
integer
• Sequence is left
operand, number is
right
• General format:
list * n
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
list1 = [1, 2, 3, 4]
list2 = [item for item in list1]
list1 = [1, 2, 3, 4]
list2 = [item**2 for item in list1]