Chapter 3: Lists Flashcards

1
Q

create a simple list and print out the list as a whole

A

bicycles = [‘trek’, ‘Cannondale’, ‘redline’, ‘specialized’]

print(bicycles)

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

access every element in the list individually and print them out

A

bicycles = [‘trek’, ‘Cannondale’, ‘redline’, ‘specialized’]

print(bicycles[0])

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

use string methods on each of the elements of the list to change the case of the elements

A

print(bicycles[0].title())

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

access the last element in the list

A

print(bicycles[-1])

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

create an f-string in a variable to put an element of a list into the string. then print the string out

A

bicycles = [‘trek’, ‘Cannondale’, ‘redline’, ‘specialized’]
var = f”I really like the bikes made by {bicycles[0].title()}”)
print(var)

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

modify an element in a list

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
motorcycles[0] = ‘ducati’
print(motorcycles[0])

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

add an element to the list using .append()

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
motorcycles.append(‘ducati’)
print(motorcycles)

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

create another empty list and then add elements to it

A
var = []
var.append('    ')
var.append('    ')
var.append('    ')
print(var)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

use insert to add an element anywhere in the list

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
motorcyles.insert(2, ‘ducati’)
print(motorcycles)

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

use the delete function to remove an item from anywhere in the list

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
del motorcycles[0]
print(motorcycles)

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

use the pop method to move an element from a list to a variable

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
popped_cycle = motorcycles.pop()
print(popped_cycles)

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

add a popped variable to an f-string

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
popped_cycle = motorcycles.pop()
print(f”The last motorcycle I owned was a {popped_cycle.title()}”)

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

pop an item from anywhere in a list

A
motorcycles = ['honda', 'yamaha', 'suzuki']
popped_cycle = motorcycles.pop(0)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

use remove to remove an element by its value

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]

motorcycles.remove(‘honda’)

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

use .remove to remove an item from the list using a variable that contains that value

A

motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’, ducati’]
too_expensive = ‘ducati’
motorcycles.remove(too_expensive)
print(motorcycles)

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

sort a list alphabetically

A

cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
cars.sort()
print(cars)

17
Q

use .sort to sort the original list in reverse

A

cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]

cars.sort(reverse=True)

18
Q

use sorted to temporarily sort a list

A

cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
print(“here is the original list:”)
print(cars)

print(“\nHere is the sorted list:”)
print(sorted(cars))

print(“\nhere is the original list again:”)
print(cars)

19
Q

print a list in reverse using .reverse

A

cars.reverse()

print(cars)

20
Q

find the length of the list

A

print(len(cars))