Chapter 4- Lists Flashcards
Indexing list
>>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[0] 'cat' >>> spam[1] 'bat' >>> spam[2] 'rat' >>> spam[3] 'elephant' >>> ['cat', 'bat', 'rat', 'elephant'][3] 'elephant' ➊ >>> 'Hello ' + spam[0] ➋ 'Hello cat' >>> 'The ' + spam[1] + ' ate the ' + spam[0] + '.' 'The bat ate the cat.'
A list is
list is a value that contains multiple values in an ordered sequence. The term list value refers to the list itself (which is a value that can be stored in a variable or passed to a function like any other value), not the values inside the list value.
Uses [ ]
Values inside the list are also called items. Items are separated with commas (that is, they are comma-delimited).
List of other lists
>>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]] >>> spam[0] ['cat', 'bat'] >>> spam[0][1] 'bat' >>> spam[1][4] 50
Negative indexing
> > > spam = [‘cat’, ‘bat’, ‘rat’, ‘elephant’]
spam[-1]
‘elephant’
spam[-3]
‘bat’
‘The ‘ + spam[-1] + ‘ is afraid of the ‘ + spam[-3] + ‘.’
‘The elephant is afraid of the bat.’
Slicing lists
>>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[0:4] ['cat', 'bat', 'rat', 'elephant'] >>> spam[1:3] ['bat', 'rat'] >>> spam[0:-1] ['cat', 'bat', 'rat']
Len()
What is length of
spam = [‘cat’, ‘bat’, ‘rat’, ‘elephant’]
2spam = [[‘cat’, ‘bat’], [10, 20, 30, 40, 50]
spam. len()
2spam. len()
Asiago g values within lists
>>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[1] = 'aardvark' >>> spam ['cat', 'aardvark', 'rat', 'elephant'] >>> spam[2] = spam[1] >>> spam ['cat', 'aardvark', 'aardvark', 'elephant'] >>> spam[-1] = 12345 >>> spam ['cat', 'aardvark', 'aardvark', 12345]
Del()
will delete values at an index in a list. All of the values in the list after the deleted value will be moved up one index
>>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> del spam[2] >>> spam ['cat', 'bat', 'elephant'] >>> del spam[2] >>> spam ['cat', 'bat']
Cat naming programming
Go review
Iterating over a list
> > > supplies = [‘pens’, ‘staplers’, ‘flame-throwers’, ‘binders’]
for i in range(len(supplies)):
print(‘Index ‘ + str(i) + ‘ in supplies is: ‘ + supplies[i])
Multiple variable assignment What'd easier than >> cat = ['fat', 'orange', 'loud'] >>> size = cat[0] >>> color = cat[1] >>> disposition = cat[2]
> > > cat = [‘fat’, ‘orange’, ‘loud’]
|»_space;> size, color, disposition = cat
Augmented variables
spam += 1
spam = spam + 1
spam -= 1
spam = spam - 1
spam *= 1
spam = spam * 1
spam /= 1
spam = spam / 1
spam %= 1
Spam =spam%1
Methods
Called on values
Similar to functions
Index()
First appearance is returned
>>> spam = ['hello', 'hi', 'howdy', 'heyas'] >>> spam.index('hello') 0 >>> spam.index('heyas') 3 >>> spam.index('howdy howdy howdy') Traceback (most recent call last): File "", line 1, in spam.index('howdy howdy howdy') ValueError: 'howdy howdy howdy' is not in list
Append()
> > > spam = [‘cat’, ‘dog’, ‘bat’]
spam.append(‘moose’)
spam
[‘cat’, ‘dog’, ‘bat’, ‘moose’]
Adds to the end of list
Insert()
> > > spam = [‘cat’, ‘dog’, ‘bat’]
spam.insert(1, ‘chicken’)
spam
[‘cat’, ‘chicken’, ‘dog’, ‘bat’]
Return value of insert(), append(),index()
None
So you do not want to have them assigned as variables
Remove()
> > > spam = [‘cat’, ‘bat’, ‘rat’, ‘elephant’]
spam.remove(‘bat’)
spam
[‘cat’, ‘rat’, ‘elephant’]
When do you use del() and when do you use remove()
Del() when you know index
Remove() when you know value
Sort()
>>> spam = [2, 5, 3.14, 1, -7] >>> spam.sort() >>> spam [-7, 1, 2, 3.14, 5] >>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants'] >>> spam.sort() >>> spam ['ants', 'badgers', 'cats', 'dogs', 'elephants']
Keyword for sort()
> > > spam.sort(reverse=True)
spam
[‘elephants’, ‘dogs’, ‘cats’, ‘badgers’, ‘ants’]
3 things about sort()
- no return value
- cant sort integers and strings
- sorts in ASCII betical order so uppercase comes before lower