ITP 115 Lecture Notes - Lecture 8: Delimiter

34 views3 pages
7 Feb 2017
School
Course
Professor

Lecture 8
2/2/17
Often, we want to create an empty list before a loop, at the start of our program, etc.
syntax:
numbers = list()
Or
numbers = []
Lists are mutable!
o Assign a new list element by index
o Assign a new list slice
Replace multiple items with 1 item
o Delete a list element
Doesn’t create a gap in a sequence
All the elements “slide down” 1 position
o Delete a list slice
Delete multiple elements
example
Nums = [3, -12, 5]
3, -12, 5
Nums[0] = 46
46, -12, 5
Nums[0:2] = [7,9]
7, 9, 5
Nums[0:2] = [13]
13, 5
List methods
o someList.append(value)
Adds value to end of list
o someList.sort()
Sorts values, smallest to largest
o someList.reverse()
Reverses order of list
o someList.count(value)
Returns # of occurrences of value
o someList.index(value)
Returns the 1st position number of where the value occurs
o someList.insert(i, value)
Inserts value at position i
o someList.pop([i])
Returns value at position i and removes value from list
Providing position number i is optional → without it, the last element in
the list is removed and returned
o someList.remove(value)
Removes 1st occurrence of value from the list
o del someList[i]
Removes the element at the specified index
someList.sort()
o Sorts elements, smallest 1st
Sorts the actual list; doesn’t return a new list
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Get access

\$10 USD/m
Billed \$120 USD annually
Homework Help
Class Notes
Textbook Notes