what does .insert do
A list method to insert a specific index in a list
what 2 inputs are used for .insert
2.The element you want to insert at the specified index.
example
store_line = [“Karla”, “Maxium”, “Martim”, “Isabella”]
store_line.insert(2, “Vikor”)
print(store_line)
output: = [‘Karla’, ‘Maxium’, ‘Vikor’, ‘Martim’, ‘Isabella’]
What is the function of .pop
A list method to remove a specific index or from the end of the list
what is the single input .pop requires
The index for the element you want to remove.
example:
[‘Python’, ‘Data Structures’, ‘Balloon Making’, ‘Algorithms’]
cs_topics.pop(2)
print(cs_topics)
Would output:
[‘Python’, ‘Data Structures’, ‘Algorithms’]
how do we find what element was deleted using .pop
imply assign a variable to the call of the .pop() method. In this case, we assigned it to removed_element.
example:
removed_element = cs_topics.pop()
print(cs_topics)
print(removed_element)
Would output:
[‘Python’, ‘Data Structures’, ‘Balloon Making’, ‘Algorithms’]
‘Clowns 101’
We don’t have to save the removed value to any variable if we don’t care to use it later.
how do we target the end of a list using .pop
dont add any index number in the .pop just keep it .pop()
what is range() method
Creates a sequence of intagers
how do you create a range
So, if we want the numbers from 0 through 9, we use range(10) because 10 is 1 greater than 9:
my_range = range(10)
print(my_range)
Would output:
range(0, 10)
how do we make it so the range object becomes a list when we print it
The list() function takes in a single input for the object you want to convert.
We use the list() function on our range object like this:
print(list(my_range))
Would output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
what inputs can you give a .range( ?, ?, ? )
If we use a third input, we can create a list that “skips” numbers.
For example, range(2, 9, 2) will give us a list where each number is 2 greater than the previous number:
my_range2 = range(2, 9, 2)
print(list(my_range2))
Would output:
[2, 4, 6, 8]
what is the len() function
we’ll need to find the number of items in a list, usually called its length.
We can do this using a built-in function called len().
how do we apply the len( ) function
When we apply len() to a list, we get the number of elements in that list:
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
Would output:
5
how do you Calculate the length of list and save it to a variable
long_list = [1, 5, 6, 7, -23, 69.5, True, “very”, “long”, “list”, “that”, “keeps”, “going.”, “Let’s”, “practice”, “getting”, “the”, “length”]
ong_list_len = len(long_list)
print(long_list_len)
what is slicing a list
In Python, often we want to extract only a portion of a list. Dividing a list in such a manner is referred to as slicing.
how do you slice a list
explain example of slice
Take the list fruits as our example:
fruits = [“apple”, “cherry”, “pineapple”, “orange”, “mango”]
If we want to select the first n elements of a list, we could use the following code:
fruits[:n]
For our fruits list, suppose we wanted to slice the first three elements.
The following code would start slicing from index 0 and up to index 3. Note that the fruit at index 3 (orange) is not included in the results.
print(fruits[:3])
Would output:
[‘apple’, ‘cherry’, ‘pineapple’]
what does .count method do
counts occurrences of an item in a list.
how to use .count
letters = [“m”, “i”, “s”, “s”, “i”, “s”, “s”, “i”, “p”, “p”, “i”]
If we want to know how many times i appears in this word, we can use the list method called .count():
num_i = letters.count(“i”)
print(num_i)
Would output:
4
how can you use .count() to count element appearances in a two-dimensional list.
Let’s use the list number_collection as an example:
number_collection = [[100, 200], [100, 200], [475, 29], [34, 34]]
If we wanted to know how often the sublist [100, 200] appears:
num_pairs = number_collection.count([100, 200])
print(num_pairs)
what does .sort do
Often, we will want to sort a list in either numerical (1, 2, 3, …) or alphabetical (a, b, c, …) order.
We can sort a list using the method .sort().
what does .sort(reverse=True)
.sort() also provides us the option to go in reverse. Instead of sorting in ascending order like we just saw, we can do so in descending order.
names.sort(reverse=True)
print(names)
what does sorted do ( different to .sort )
it comes before a list, instead of after as all built-in functions do.
It generates a new list rather than modifying the one that already exists.
what does the .zip function do
The zip() function allows us to quickly combine associated data-sets without needing to rely on multi-dimensional lists.
The zip() function takes two (or more) lists as inputs and returns an object that contains a list of pairs. Each pair contains one element from each of the inputs.