lists 2 Flashcards

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

what does .insert do

A

A list method to insert a specific index in a list

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

what 2 inputs are used for .insert

A
  1. 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’]

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

What is the function of .pop

A

A list method to remove a specific index or from the end of the list

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

what is the single input .pop requires

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how do we find what element was deleted using .pop

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how do we target the end of a list using .pop

A

dont add any index number in the .pop just keep it .pop()

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

what is range() method

A

Creates a sequence of intagers

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

how do you create a range

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how do we make it so the range object becomes a list when we print it

A

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]

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

what inputs can you give a .range( ?, ?, ? )

A

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]

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

what is the len() function

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how do we apply the len( ) function

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how do you Calculate the length of list and save it to a variable

A

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)

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

what is slicing a list

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how do you slice a list

A
  • We can do this using the following syntax: letters[start:end], where:
  1. start is the index of the first element that we want to include in our selection. In this case, we want to start at “b”, which has index 1.
  2. end is the index of one more than the last index that we want to include. The last element we want is “f”, which has index 5, so end needs to be 6. index needs to be one above the number you want to end on
17
Q

explain example of slice

A

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’]

18
Q

what does .count method do

A

counts occurrences of an item in a list.

19
Q

how to use .count

A

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

19
Q

how can you use .count() to count element appearances in a two-dimensional list.

A

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)

20
Q

what does .sort do

A

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().

21
Q

what does .sort(reverse=True)

A

.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)

22
Q

what does sorted do ( different to .sort )

A

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.

23
Q

what does the .zip function do

A

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.

24
Q

why would the output show a list of random letters and numbers

names_and_heights = zip(names, heights)
print(names_and_heights)

A

zip object at 0x7f1631e86b48>

This zip object contains the location of this variable in our computer’s memory.

25
Q

How would be convert the random letters and numbers outputed by using the .zip function using list

A

it is fairly simple to convert this object into a useable list by using the built-in function list():

converted_list = list(names_and_heights)
print(converted_list)

Outputs:

[(‘Jenny’, 61), (‘Alexus’, 70), (‘Sam’, 67), (‘Grace’, 64)]

26
Q

how would we use .zip to combine two data sets

A

data set:
names = [“Jenny”, “Alexus”, “Sam”, “Grace”]
heights = [61, 70, 67, 64]

solution:
names_and_heights = zip(names, heights)

27
Q

why does the .zip create round brackets instead of square brackets

[(‘Jenny’, 61), (‘Alexus’, 70), (‘Sam’, 67), (‘Grace’, 64)]

A

1.Our data set has been converted from a zip memory object to an actual list (denoted by [ ])

2.Our inner lists don’t use square brackets [ ] around the values. This is because they have been converted into tuples (an immutable type of list).

28
Q

what is a tuples in python

A

Python tuples are a type of data structure that is very similar to lists. The main difference between the two is that tuples are immutable, meaning they cannot be changed once they are created. This makes them ideal for storing data that should not be modified, such as database records.

29
Q
A