Lists Flashcards
Build this list [0,0,0] two separate ways
- [0]*3
- list2 = [0,0,0]
list2 - s = 1,0,0,0
s[1:]
Reassign ‘hello’ in this nested list to say ‘goodbye’ instead.
List3 = [1,2,[3,4,’hello’]]
- List3 [2][2] = ‘goodbye’
List3
Sort the list below:
list4 = [5,3,4,6,1]
- list4.sort()
list4 - sorted(list4)
What is the append method?
The append method is used to permanently add an item to the end of a list:
IN) List1 = [1,2,3]
IN) list1.append(‘added’)
IN) list1
OUT) [1,2,3, ‘added’]
list1 = [1,2,3, ‘this is a string’]
grab 3
List1.pop(2)
3
What is does .pop() do?
.pop() removes the last index by default but you can stiil specify which index to pop off.
sort this list in alphabetical order
new_list =[ ‘a’, ‘j’, ‘e’, ‘x’, ‘r’, ‘c’]
new_list.sort()
Sort this list in reverse order:
new_list=[ ‘a’, ‘j’, ‘e’, ‘x’, ‘r’, ‘c’]
new_list.reverse
What does it mean by nesting?
Nesting means that you can have data structures within data structures e.g. a list inside a list
Make a matrix with three lists
list_1=[1,2,3]
list_2=[4,5,6]
list_3=[7,8,9]
—
matrix = [list_1,list_2,list_3]
[[1,2,3], [4,5,6], [7,8,9]]
a. Grab the first item in the matrix object
b. Grab the first item of the first item in the matrix object
a. matrix[0]
[1,2,3]
b. matrix[0][0]
1
What is unique about a set?
They don’t allow for duplicate items
Use a set to find the unique values in the list below:
list5 = [1,2,2,33,4,4,11,22,3,3,2]
set(list5)
1,2,3,4,11,22,33
Show two of tuples built-in methods
.index to enter a value and return index
.count to count the number of times a value appears
In Python, what are lists
lists are a sequence that are constructed in brackets [] and commas seperating every element in the list