lists 2 Flashcards
what does .insert do
A list method to insert a specific index in a list
what 2 inputs are used for .insert
- The index you want to insert into.
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.