Lists Flashcards

1
Q

List are:

A
  • 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 ave 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