Lists Flashcards

1
Q

What is a list and how do you create one?

A

A list is a simple data structure that can store items of any data type. Lists also know as an array stores and references items by index. The index start at zero and indicates the items position in the list:

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

How do you find the number of items in a list?

A

Pass the list as an argument to the ‘len()’ function

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

Can items in a list at a specific index be mutated?

A

Yes. simply assign the new value directly to the desired index. Note that this will replace any value currently at that index.

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

What does the ‘append()’ method do when used with a list?

A

The append method adds the given argument to the end of the list.

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

What does the ‘.pop()’ method do when used with a list?

A

The pop method removes the last item in the list and returns it. It can also take an optional argument which is the index of the element you wish to remove.
my_list.pop(2)

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

How does the .insert method work when used on a list?

A

The ‘.insert’ method is called on the list, it expects a minimum of two inputs the index for insertion and the element to be inserted.

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

When accessing a list

alternative to the ‘indexed for-loop’?
when should you use it?

A

If you do not need to update the item(s) in a list i.e. you only need to access the item and not its index you can use the ‘for-in’ syntax

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

Using slice syntax “return a slice of the list from index 1, up to but not including 5, skipping every 2nd value.

A

Note that slicing returns a new list.

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

How do we concatenate lists together?

A

Simply use the ‘+’ operator. Notice that the first level of square brackets are removed i.e. the elements of the list are added not the list it self.

you can also use the += operator or call the extend() method on a list.

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

How to check for an item in a list without using a ‘if-statement’?

A

By using the ‘in’ keyword. This will return a Boolean value.

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

How do we delete specific items from a list?

A

Use the ‘del’ keyword followed by the name of the list square brackets containing the index or slices you wish to delete.
The ‘.pop()’ method with a index as an argument can also be used.

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

How does the ‘.count()’ method work?

A

The ‘.count()’ method will count the number of times an element appears in a list.

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

How does the ‘.sort()’ method work?

A

The ‘.sort()’ method will sort a list alphabetically or numerically.
Calling the method on a list with no inputs sorts the list in ascending order.
my_list.sort()

To sort in descending order pass the following argument to the method.
my_list.sort(reverse=true)

Note that the ‘.sort()’ method modifies the original list and does not return any value.

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

What is the difference between the ‘.sort()’ method and ‘sorted()’ function?

A

The main difference is that the sorted() function does not modify the original list like the .sort() method does. Therefore using the sorted() function will return a value that can be stored in a variable for use.

names = [“Xander”, “Buffy”, “Angel”, “Willow”, “Giles”]
sorted_names = sorted(names)
print(sorted_names)

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

How do we easily access the last element in a list

A

use negative indexing
my_list[-1]

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

How do we append multiple items to a list?

A

use ‘+’ to concatenate two lists together: Note that the original list was not modified, only lists can be concatenated this way.

17
Q

What is a list comprehension?

A

A list comprehension is a concise way to create lists in Python. It provides a compact syntax for generating a new list by iterating over an existing iterable (such as a list, tuple, set, or even a string), applying an expression to each element, and optionally including only certain elements based on a condition.

18
Q

Basic list comprehension syntax

A
output: [2, -2, 158, 66, -90]
19
Q

list comprehension with condition and expression syntax

A

The syntax for the list comprehension changes based on conditional statement and it’s complexity.

20
Q

How to remove an item from a list by name not index?

A

Use the remove() method with the item as the argument.

21
Q

How would we remove an item from a list?

A

Use the .remove() method.
Note this method will remove the first occurrence of the list item.
Attempting to remove an element that does not exist will result in a valueError!

22
Q

What is a two dimensional list?

A

A two dimensional list is a ‘list of lists’ i.e.
a list whose elements are other lists.
A two dimensional list is often a good data structure to represent a grid or table of data.

To select an element from the inner list use the double [] bracket syntax.
tic_tac_toe[0][1] # ‘O ‘ (index 1 of array 0)