Lists Flashcards

1
Q

What is a list[]

example = [“dd”, “ff”]

A

Collection of items in a particular order

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

A list that holds “trek” “cannondale” “redline” “specialized”

print the array no(which holds these elements)

A

no = [“trek”, “cannondale” , “redline” , “specialized”]

print(no)

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

Print the 3rd item in a list

no = [“trek”, “cannondale” , “redline” , “specialized”]

A

print(no[2])

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

Individual values from a list

no = [“trek”, “cannondale” , “redline” , “specialized”]

Print the first item from the list, assign message to equal it, and print it

A

message = f”My first bike was a {no[0].title()} bike “

print(message)

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

Modify elements in a list

bikes = [‘nice’, ‘cool’, ‘wins’, ‘unck’]

Change the ‘wins’ to ‘loses’

A

bikes[2] = ‘loses’

print(bikes[2])

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

Adding(appending) elements to the end of a list

append() method for bikes = [‘nice’, ‘cool’, ‘wins’, ‘unck’]

A

bikes.append(‘more’)

print(bikes)

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

Add elements to an empty list

nicebikes = []

A

nicebikes. append(‘red’)
nicebikes. append(‘black’)
nicebikes. append( ‘white’)

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

insert elements into a list insert(index, element)

nicebikes = [‘red’, ‘black’, ‘white’]

Insert at the index postion 2 ‘blue’

Output: [‘red’, ‘black’, ‘blue’, ‘white’]

A

nicebikes.insert(2, ‘blue’)

print(nicebikes)

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

Remove an item from a list

A

del nicebikes[2]

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

Pop() method to remove an item

bikes = [‘nice’, ‘cool’, ‘wins’, ‘unck’]

  • Pop ‘nice’ at index 0 defined as first_owned variable
  • Print “I popped the nice bike”
A

first_owned = bikes.pop(0)

print(f”I popped the {first_owned.title() } bike “)

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

When should you use the del vs the pop() method?

A

-Delete is permeantenly get rid of the itme, pop to use the item as you remove it

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

Remove() method

-How can it be used?

bikes = [‘nice’, ‘cool’, ‘wins’, ‘unck’]

-Remove ‘wins’ from bikes with the remove() method

A
  • Removes a string, interger or decimal value from a list
    bikes. remove(‘wins’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Permanenetly sort a list(alphabetically)

cars = [‘bad’ , ‘nope’ , ‘mo’]

A

cars.print()

print(cars)

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

temporarily sort a list

cars = [“how”, “very”, “cool”]

A

print(sorted(cars))

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

Reverse a list permanenetly

bikes = [‘nice’, ‘cool’, ‘wins’, ‘unck’]

A

bikes.reverse()

print(bikes)

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

Sort a list temporarily

cars

Print cars as a temporarily sorted list

A

print(sorted(cars))

17
Q

permanenetly print a list in reverse order

cars = [“mp”, “jgh”, “hh”]

A

print(cars)

cars.reverse()

print(cars)

18
Q

finding the lenght of the list

bikes = [‘nice’, ‘cool’, ‘wins’, ‘unck’]

A

len(bikes)

19
Q

loop through an entire list

animes = [“opm”, “bnha”, “

OP”]

A

for grom in animes:

print(animes)

20
Q

alist = [“nk”, “rk”, “bk”]

for variable in alist:

print(alist)

Output:

A

nk

rk

bk

21
Q

magicians = “Alice”, “Andrew”, “Sarah”]

print a message to each magician

A

for magician in magicians:

print(f”Congrats {magician} , you did well!”)

22
Q

Forgetting to indent Additional lines

A
23
Q
A
24
Q
A
25
Q
A
26
Q
A
27
Q
A
28
Q
A
29
Q
A
30
Q
A
31
Q
A
32
Q
A
33
Q
A