Lists Flashcards

1
Q

What is a list?

A

a collection of items in a particular order

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

Access the first element of the list ‘name_list’

A

name_list[0]

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

Access the last item in the list ‘name_list’

A

name_list[-1]

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

Change the third item of the list ‘name_list’

A

name_list[2] = ‘George’

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

Append a new item at the end of ‘name_list’

A

name_list.append(“Mary”)

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

Add a new item in the list ‘name_list’ in the fourth position

A

name_list.insert(3, “Francesco”)

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

When you use the .insert() method on a list, what happens to all the values that come after the inserted value?

A

They are all shifted one position to the right

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

Remove the 5th item from ‘name_list’

A

del name_list[4]

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

Remove the last item from ‘name_list’ and assign it to variable ‘user’

A

user = name_list.pop()

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

Remove first item from ‘name_list’ and assign it to variable ‘user’

A

user = name_list(0)

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

Remove user “George” from ‘name_list’ and assign it to variable ‘user’

A

user = name_list.remove(“George”)

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

Order the items of ‘name_list’ alphabetically and then reverse the order

A

name_list.sort()

name_list.sort(reverse=True)

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

Display the items in ‘name_list’ in alphabetical order without actually changing the order of the list

A

print(sorted(name_list))

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

Reverse the order of the items in list ‘name_list’

A

name_list.reverse()

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

Get the number of items in list ‘name_list’

A

len(name_list)

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

Print all the items in list ‘name_list’

A

for name in name_list:

print(name)

17
Q

Print numbers from 1 to 5 on separate lines

A

for value in range(1, 6):

print(value)

18
Q

Print every other value from 1 to 10 on separate lines

A

for value in range(1, 11, 2):

print(value)

19
Q

Create a list of numbers from 1 to 20 and assign it to ‘numbers’

A

numbers = list(range(1, 21))

20
Q

What is a list comprehension?

A

A list comprehension combines the for loop and the creation of new elements into one line, and automatically appends each new element

21
Q

Create a list of squares for each number ranging from 1 to 10, and assign it to variable ‘squares’

A

squares = [value ** 2 for value in range(1, 11)]

22
Q

What is slicing?

A

Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the specific range of elements by mentioning their indices. (source: simplilearn.com)

23
Q

Make a slice of the first 5 items in ‘name_list’ and assign it to ‘first_users’

A

first_users = name_list[0:5]

24
Q

Make a slice of the last 5 items in ‘name_list’ and assign it to ‘last_users’

A

last_users = name_list[-5:]

25
Q

Print the last 3 items in ‘name_list’ on separate lines

A

for name in name_list[-3:]:

print(name)

26
Q

Make a copy of ‘name_list’ and call it ‘users’

A

users = name_list[:]

27
Q

What is a tuple?

A

A tuple is a list that cannot be changes (is immutable)

28
Q

Define a tuple named ‘length’ with only one element (120).

A
length = (120, )
#notice the trailing comma, which is mandatory
29
Q

Get the last value in tuple ‘length’ and assign it to “closet”

A

closet = length[-1]

30
Q

Can you modify the items of a tuple?

A

You can’t modify a tuple, but you can assign a new value to a variable that represents a tuple.