Lists in Python Flashcards

1
Q

List syntax

A

Var=[‘smth’, ‘smth again’, ‘smth else’]

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

How to access elements in the List?

A

print=(list_name[index position])

[-index] - last items

Index position starts from 0 not 1

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

How to modify the value of the 1st item?

A

var[0]=”new value”

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

How to add element to the list?

A

var.append(‘new element’)

Add element to the end of the list

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

How to add element at any position to the list?

A

variable.insert(index, ‘element’)

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

How to remove item from the list?

A

del var[index]

In case if the index, position of the item is known

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

Which method we use when we want to use the value of the item after we remove it?

A

pop ()

Removes the last item in a list

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

How to remove an item from any position?

.pop()

A

.pop(index)

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

How to remove the item if index is not known, but only the value?

A

.remove(‘value’)

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

How to sort a list permanently?

(Alphabetically)

A

.sort()

sort(reverse=true)

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

How to sort list temporarily?

Maintain original order of the list, but present in a sorted order

Alphabetically

A

sorted() & reverse()

Function!

print(sorted(list_name))

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