Week 12: Arrays Flashcards
T or F:
Variables need to be numbered when in Lists.
False.
They are automatically numbered (called index).
How to create a new List
Variable_list_name = [“variable1”, “variable 2”, “variable 3”]
T or F:
Lists can comprise of mixed types of data.
True.
Numbers and texts.
How to add a variable to an existing list?
How to add more than 1?
variable_list = [“variable4”, “variable5”]
variable_list.append(“variable6”)
______
variable_list.extend() or +
Using an existing list, how to add more variables for a bigger/new/other list.
Longer_variable_list=variable_list_name + [“variable4”, “variable5”]
To add a new variable in a list in a specific spot (index).
variable_list_name.insert(2, “variable 6”)
Assign a new value to an element (replace).
variable_list_name [2] = “variable 7”
How to create an empty/editable list.
new_list = []
To add to the new list;
new_list = new_list + [“variable1”]
To copy some elements from 1 list into another.
newest_list = variable_list_name [2:5]
It details how many elements will be assigned to the new list, this case is 3
What is its meaning, to take slices?
Copy, not cut from lists
When the last element of the slice is the last of the original list, can omit the second number.
newest_list = variable_list_name [2:]
T or F:
Index’s count 0 as the 1st #
True.
Example, 0-5 would be 0, 1, 2, 3, 4
How to delete an element in a list?
del variable_list_name [index number]
The index moves to fill the gap of that, so if you deleted index 0, the next in line would take its place.
OR
Variable_list_name.remove(“variable3”)
How to remove an element from 1 list and place at the end of another.
Newer_list = variable_list_name.pop(index number)
OR
Newer_list.append(variable_list_name.pop(index number))
How to remove an element from 1 list and place it in a specific index of another list.
Newer_list.insert((spot), list_name.pop(1))
Example, task of index 3 (spot 2) will be added removed and added to another list:
tasks_accomplished.insert(1, tasks.pop(1))
What is a tuple()?
Non-editable/stable list.
How do you find a variable in a list?
for loop.
Example:
city_to_check = “Tucson”
new variable is a_clean_city
cleanest_cities = [“we”, Tucson”, “honolulu”]
for a_clean_city in cleanest_cities:
if city_to_check == clean_cities:
print(“Its one of the cleanest cities”)
How can you stop loops?
break
How to search through a specific range of index numbers?
for x in range(1,11)
print(x)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
How to search for a specific variable in a list?
for num in nums:
if nums == 3:
print(“found 3!”)
break
print(num)
1, 2, found 3!