Lists Flashcards

1
Q

List are:

A
  • A list is a collection of items in a particular order
  • Ordered sequences that can hold a variety of object types.
  • [] Brackets and commas to separate objects in list [1,2,3,4,5]
  • List support indexing and slicing
  • List can be nested and also have a variety of useful methods that ce called off of them.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List examples:

A

my_list = [1,2,3,4]

my_list2 = [‘STRING’,100,23.3]

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

Cancatinate 2 list

mylist = [‘one’,’two’,’three’]

another_list = [‘apple’,’pie’,’cherry’]

A

mylist + another_list

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

Combine two list

mylist = [‘one’,’two’,’three’]

another_list = [‘apple’,’pie’,’cherry’]

A

mynewlist = mylist + another_list

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

Change the first string in this list to “One for all’

new_list = [‘one’, ‘two’, ‘three’, ‘four’, ‘five’]

A

new_list[0] = ‘One for all’

would change it to

new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’]

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

Add an element to the end of a list:

A

new_list.

new_list.append(‘six’)

new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’]

NOTE:This affects the list in place

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

Remove the last element from a list:

A

Done using pop

new_list.pop()

new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’]

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

Remove an element from the 3 position

new_list = [‘One for all’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’]

A

new_list.pop(3)

new_list = [‘One for all’, ‘two’, ‘three’, ‘five’, ‘six’]

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

Sort list

new_list = [‘a’, ‘e’, ‘x’, ‘b’, ‘c’]

num_list = [4,1,8,3]

A

Call the sort method

new_list.sort()

NOTE:This sorts the list in place

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

Reassign this list but first Sort list

new_list = [‘a’, ‘e’, ‘x’, ‘b’, ‘c’]

num_list = [4,1,8,3]

A

new_list.sort()

my_sorted_list = new_list

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

Reverse this list

new_list = [‘a’, ‘e’, ‘x’, ‘b’, ‘c’]

num_list = [4,1,8,3]

A
  • Call the reverse method
  • num_list.reverse()

[3,8,1,4]

  • new_list.reverse()

[‘c’, ‘b’, ‘e’, ‘a’]

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

How do I index a nested list? For example if I want to grab 2 from [1,1,[1,2]]?

A

You would just add another set of brackets for indexing the nested list, for example:

my_list[2][1]

NOTE: List follow the same numbering sequence as strings.

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

If lst=[0,1,2] what is the result of lst.pop()

A

2

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

Lists can have multiple object types.

A

True

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

If lst=[‘a’,’b’,’c’] What is the result of lst[1:]?

A

[‘b’,’c’]

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

Are list mutable?

A
  • Yes, Python lists are mutable
  • which means you can modify their content (add, remove, or change elements) after creation.
    *
17
Q

What are the immutable data types in Python?

A
  • Strings
  • Tuples
  • Numbers (integers, floats)
18
Q

What are the things inside a list called?

A

Each item is called an Element

19
Q

Insert an element into a list

A
  • You can add a new element at any position in your list by using the insert() method.
  • You do this by specifying the index of the new element and the value of the new item:
motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati')
print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
20
Q

What are the 2 things need to Insert an element into a list?

A
  • Index where it is going
  • New element
motorcycle.insert(3, "harley davidson")
print(motorcycle)
['suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
motorcycle.insert(0, "triumph")
print(motorcycle)
['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
21
Q

How do remove an element from a list?

A

Delete an element permanently a list

  • **del **Statement (This is not a method)
del motorcycle[2]
motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']

print(motorcycle)
['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
del motorcycle[2]
print(motorcycle)
['triumph', 'suzuki', 'kawasiki', 'harley davidson', 'honda']
  • ‘yamaha’ is now missing
22
Q

How do remove the last item from a list for later use?

A
  • pop() method
motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
print(motorcycle)
['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
popped_motorcycle = motorcycle.pop()
print(f"The last motorcyle I owneed was a {popped_motorcycle.title()}.")
The last motorcyle I owneed was a Honda.
print(popped_motorcycle)
honda
23
Q

How do remove an item from anywhere in list for later use?

A
  • pop() method
# Remove an element for later use in a list from any where in the list
motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']
print(motorcycle)
['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda']

popped_motorcycle = motorcycle.pop(3)
print(f"The last motorcyle I owneed was a {popped_motorcycle.title()}.")
The last motorcyle I owneed was a Kawasiki.
print(popped_motorcycle)
kawasiki
24
Q

What is the difference between append() and insert()?

A
  • **appened() **will add an item to the end of a list
  • insert() will add an element anywhere in a list
25
If you use **remove()**, how many times will it try to remove an item?
* The **remove() **method deletes only the first occurrence of the value you specify. * If there’s a possibility the value appears more than once in the list, you’ll need to use a loop to make sure all occurrences of the value are removed.
26
Remove an item from a list using the value?
* **remove()** method ``` # Remove an element by vaule from a list motorcycle = ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda'] print(motorcycle) motorcycle.remove('yamaha') ['triumph', 'suzuki', 'yamaha', 'kawasiki', 'harley davidson', 'honda'] # print(f"The last motorcyle I owneed was a {popped_motorcycle.title()}.") print(motorcycle) ['triumph', 'suzuki', 'kawasiki', 'harley davidson', 'honda'] ``` * 'yamaha' is now missing
27
What is sort()?
* **sort() **is a method * The **sort()** method changes the order of the list permanently. * You can also sort this list in reverse-alphabetical order by passing the argument reverse=True to the sort() method.
28
How do you sort, a **sort()** list in reverse-alphabetical order?
* You can also sort this list in reverse-alphabetical order by passing the argument **reverse=True** to the **sort()** method. ``` cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars) ['audi', 'bmw', 'subaru', 'toyota'] cars.sort(reverse=True) print(cars) ['toyota', 'subaru', 'bmw', 'audi'] ```
29
How do you sort a list temporarily?
# print("\nHere is the sorted list:") * **sorted() ** function * Lets you display your list in a particular order, but doesn’t affect the actual order of the list. ``` cars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) ['bmw', 'audi', 'toyota', 'subaru'] print(sorted(cars)) ['audi', 'bmw', 'subaru', 'toyota'] # print("\nHere is the original list again:") print(cars) ['bmw', 'audi', 'toyota', 'subaru'] ```
30
What is the difference between **sort()** and **sorted()**?
1. **sort()** is a method, meaning it is attahced to the variable 2. sort() is permanent change to a list ``` cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars) ['audi', 'bmw', 'subaru', 'toyota'] ``` 1. **sorted()** is a Function ``` # Original list cars = ['bmw', 'audi', 'toyota', 'subaru'] print(sorted(cars)) # Here is the sorted list: ['audi', 'bmw', 'subaru', 'toyota'] # Here is the original list again: ['bmw', 'audi', 'toyota', 'subaru'] ```
31
Copy a list
* First, we make a list of the foods we like called my_foods. * Then we make a new list called friend_foods. * We make a copy of my_foods by asking for a slice of my_foods without specifying any indices ❶, and assign the copy to friend_foods. * When we print each list, we see that they both contain the same foods: ``` my_foods = ['pizza', 'falafel', 'carrot cake'] ❶ friend_foods = my_foods[:] print("My favorite foods are:") print(my_foods) My favorite foods are: ['pizza', 'falafel', 'carrot cake'] print("\nMy friend's favorite foods are:") print(friend_foods) My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake'] ```