lists Flashcards

1
Q

how would you make a list of the heights of students in a class:
using -
Noelle is 61 inches tall
Ava is 70 inches tall
Sam is 67 inches tall
Mia is 64 inches tall

A
  1. set varible to height
  2. height = [61, 70, 67, 64]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What is a list in python

A

a list is one of the many built-in data structures that allows us to work with a collection of data in sequential order.

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

what does a list need to start and end with

A

1.A list begins and ends with square brackets ([ and ])

  1. Each item (i.e., 67 or 70) is separated by a comma (,)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

can you combine mutiple data types in a list ?

A

yes

example:

mixed_list_string_number = [“Noelle”, 61]

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

can a list contain a string, integer, boolean, and float.

A

yes

example:

mixed_list_common = [“Mia”, 27, False, 0.5]

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

Why would we create an empty list ?

A

Usually, it’s because we’re planning on filling it up later based on some other input. We’ll talk about two ways of filling up a list in the next exercise.

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

how do you layout a list method

A

methods will follow the form of list_name.method(). Some methods will require an input value that will go between the parenthesis of the method ( ).

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

what is the function of the list method .append( )

A

allows us to add an element to the end of a list.

append_example = [ ‘This’, ‘is’, ‘an’, ‘example’]
append_example.append(‘list’)

print(append_example)

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

what method is used to add and expand to a list using the + symbol ?

A

items_sold_new = items_sold + [“biscuit”, “tart”]

print(items_sold_new)

Prints [‘cake’, ‘cookie’, ‘bread’, ‘biscuit’, ‘tart’]

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

How do you add a single element to a list

A

f we want to add a single element using +, we have to put it into a list with brackets ([]):

my_new_list = my_list + [4]
print(my_new_list)
# Prints [1, 2, 3, 4]

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

in pyhton what do we call the location of an element in a list

A

Index

Here are the index numbers for the list calls:
Element Index
“Juan” 0
“Zofia” 1
“Amare” 2
“Ezio” 3
“Ananya” 4

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

what index number does the first element =

A

it will always start on 0

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

how do you select a single element from a list ?

A

by using square brackets ([]) and the index of the list item. If we wanted to select the third element from the list, we’d use calls[2]:

print(calls[2])

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

how can you select the last element of a list?

A

We can use the index -1 to select the last item of a list, even when we don’t know how many elements are in a list.

Consider the following list with 6 elements:

pancake_recipe = [“eggs”, “flour”, “butter”, “milk”, “sugar”, “love”]

If we select the -1 index, we get the final element, “love”.

print(pancake_recipe[-1])

Would output:

love

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

how do you change a value on a list

A

reassign the value using the specific index.

garden[2] = “Strawberries”

print(garden)

Will output:

[“Tomatoes”, “Green Beans”, “Strawberries”, “Grapes”]

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

how to put mutiple lists into one big list

A

heights = [[“Noelle”, 61], [“Ava”, 70], [“Sam”, 67], [“Mia”, 64]]

15
Q

how to remove items in a list

A

.remove

We could remove “Chris” by using the .remove() method:

shopping_line.remove(“Chris”)

print(shopping_line)

16
Q

what would this look like when creating a tictac toe game

A

tic_tac_toe = [
[“X”,”O”,”X”],
[“O”,”X”,”O”],
[“O”,”O”,”X”]
]

17
Q

how would you accsess single elements in a 2D list

A

Access the sublist at index 0, and then access the 1st index of that sublist.

wo-dimensional lists can be accessed similar to their one-dimensional counterpart. Instead of providing a single pair of brackets [ ] we will use an additional set for each dimension past the first.

If we wanted to access “Noelle”‘s height:

noelles_height = heights[0][1]
print(noelles_height)

Would output:

61

18
Q

how to modify a 2D list

A

We will need to modify the list to accommodate the change to our class_name_hobbies list. To change a value in a two-dimensional list, reassign the value using the specific index.

The list of Jenny is at index 0. The hobby is at index 1.
class_name_hobbies[0][1] = “Meditation”
print(class_name_hobbies)

19
Q

How do you append a tupple in pyhton ( use .append to add mutiple values )

A

Appending a Tuple: If you want to add a subject and its grade as a single entry, you need to append a tuple to the list. Here’s the correct way to do it:

python

gradebook.append((“computer science”, [100]))

20
Q

how do you target a sublist with a .method

A

you need to find the index number for the sublist

e.g gradebook[2].remove(85)

gradebook[2].append(“Pass”)

21
Q
A