lists (1D and 2D) Flashcards

1
Q

what is the name of a list called

A

identifier

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

what do lists look like

A

shoppingList = [“apple” , “banana”, “orange” , “mango”]
- always use square brackets

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

how to print specific item in the list

A

eg. print(shoppingList[2]) will print the third index

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

how to check the length of a list

A

print(len(shoppingList))
- it will print number of indexes

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

how to add items to a list

A

eg. shoppingList.append(“berries”)
- normal brackets
- method is .append
- always adds to end of the list
so now shoppingList = [“apple”, “banana” , “orange” , “mango” , “berries”]

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

how to remove items from a list

A

eg. shoppingList.remove(shoppingList[0])
- removes first item on list
-method is .remove
OR shoppingList.remove(“apple”)

so now shoppingList = [“banana” , “orange” , “mango” , “berries”]

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

how to check if an item is part of a list

A

containsbanana = “banana” in shoppingList
print(containsbanana)

if value printed is true then banana is in the list

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

how to check which index an item is in the list

A

print(shoppingList.index(“orange”))
- will print the number item orange is in the list
- method is .index

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

how to concatenate lists

A

eg. abc = [“1”, “2”, “3”] xyz = [“4”, “5”, “6”]
abc + xyz will print [“1”, “2”, “3”, “4”, “5”, “6”]

or abc += xyz makes abc = [“1”, “2”, “3”, “4”, “5”, “6”]

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

how to add something to a list at a specific index

A

eg. want to add pineapple to be the third item in list

shoppingList.insert(2, “pineapple”)
- method is .insert

so now shoppingList = [“apple”, “banana”, “pineapple”, “orange”, “mango”, “berries”]

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

what are 2D lists

A
  • lists within lists
    eg. abc = [[“1”, “2”, “3”], [“4”, “5”, “6”]]
  • index[0] consists of first list [“1”, “2”, “3”]
  • index[1] consists of second list [“4”, “5”, “6”]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to get a specific a value in a 2D list

A

eg. abc = [[“1”, “2”, “3”], [“4”, “5”, “6”]]

  • we want to find 6, which is in the second list so index[1]
  • and it is the third item in that list so index[3]

print(abc[1][2]) gives 6

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