Lists Flashcards

1
Q

What is a list

A

A data structure that helps store and manipulate an ordered collection of items.

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

List could contain what types of data?

A

Int.
Float
Strings
Other lists

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

What are the common methods for working with lists?

A

append, insert, remove, pop, clear, index, count,sort

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

What are the two main ways to create a list?

A
  1. Square brackets
    e.g x= [1,2]
  2. List function
    e.g x= list(1,2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

[1,2,3,4,5]
[0:4]
What will be the result and why

A

1234
The last item is not included when accessing list

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

How do you mutate a list

A

Specify the position you want to mutate along with the item
e.g x = [1,2,3,4]
x[2] = 7
This will give a new list such that;
x= 1,2,7,4
This differs from insert as insert only adds to the list.

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

List operators?

A
  1. Addition
  2. Multiplication
    Note: lists can not be subtracted or divided
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

x=[1,2,3]
y=[a,b,c]
x+y
What will be the output

A

[1,2,3,’a’,’b’,’c’]

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

x=[1,2,3]
x*2
What will be the output

A

[1,2,3,1,2,3]

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

How do you use the methods in lists?

A

list_name.method(variable)
Some times positions may be required as in insert
List_name.method(index, variable)

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

Pop

A

Removes the item in a given position and return it. If no index is specified, it will remove the last item in the list

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

How do you sort in descending order?

A

list.sort(reverse = true)

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