Python List and Loop Flashcards

1
Q

Lists

A

can contain multiple or individual items

You can put items of any type in a list

Separate with commas and surround with [ ]

Lists are kept in order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lists example:

A

meals = [“artichokes”, “bbq”, “chili”, “donuts”]

*Lists can contain duplicates

[“artichokes”, “bbq”, “chili”, “donuts”, “bbq”]

Good to use plural nouns for variable name (meals not meal)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Accessing Items list by Index:

meals = [“artichokes”, “bbq”, “chili”, “donuts”]

A

meals = [‘artichokes’, ‘bbq’, ‘eggs’]
print(meals[1])
bbq

list = [‘artichokes’, ‘bbq’, ‘eggs]
print (list.index(‘bbq’))
1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Adding to Lists

A

list. append( ) ## append elem at end
list. append(‘shemp’) ## append elem at end
list. insert(0, ‘xxx’) ## insert elem at index 0
list. extend([‘yyy’, ‘zzz’]) ## add list of elems at end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to code adding

A

list = [ ] ## Start as the empty list

list. append(‘a’) ## Use append() to add elements
list. append(‘b’)

print(list)
>[‘a’, ‘b’,]

or:
print(list.append(‘a’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

More Adding methods:

A

list = [‘larry’, ‘curly’, ‘moe’]

list. append(‘shemp’) ## append elem at end
list. insert(0, ‘xxx’) ## insert elem at index 0
list. extend([‘yyy’, ‘zzz’]) ## add list of elems at end

print (list)
[‘xxx’, ‘larry’, ‘curly’, ‘moe’, ‘shemp’, ‘yyy’, ‘zzz’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Removing from Lists

A

Remove item from end: pop with nothing in parentheses

list. remove(‘curly’) ## search and remove that element
list. pop(1) ## removes and returns ‘larry’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to code to remove:

A
list = ['larry', 'curly', 'moe']
print(list.remove("curly"))    or list.remove("curly") 
print(list)
>None
>list = ['larry', 'moe']

or
remove = list.remove(“curly”)
print(list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

more code to remove:

A
list = ['larry', 'curly', 'moe']
print(list.pop(1))    or list.pop(1)
print(list)
>curly
>list = ['larry', 'moe']

or
pop = list.pop( )
print(list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Sorting Lists

A

list.sort( )
print(sorted(list))

The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order. The original list is not changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to code: Sorting

A

list = [5, 1, 4, 3]
print(sorted(list))
>[1, 3, 4, 5]

or 
list = [5, 1, 4, 3]
list.sort
print(list)
>[1, 3, 4, 5]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Reverse sorting code:

A

list = [2, 1, 3, 5, 6, 4]
list.sort(reverse = True)
print(list) # >[6, 5, 4, 3, 2, 1]

or 
print(sorted(list, reverse = True)) # >[6, 5, 4, 3, 2, 1]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Getting List: Length

A

len(list)

Note: syntax is different than for append, pop, and sort

With len, list_name goes in parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to code: Length

A

meals = [‘hummus’, ‘ice cream’, ‘grits’]
print(len(meals))
>3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Loops

A

They’re great for repeating code

You decide the condition that ends the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

FOR and IN

A

The for construct – for var in list – is an easy way to look at each element in a list (or other collection). Do not add or remove from the list during iteration.

The in construct on its own is an easy way to test if an element appears in a list (or other collection) – value in collection – tests if the value is in the collection, returning True/False.