Looping techniques Flashcards

1
Q

how to loop key pair value in a dictionary?

A

for k, v in d.items():

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

how to value index, value in a sequence?

A

for i, v in enumerate(list):

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

how to loop over two or more sequence at the same time?

A

questions = [‘name’, ‘quest’, ‘favorite color’]
answers = [‘lancelot’, ‘the holy grail’, ‘blue’]
for q, a in zip(questions, answers):
print(q,a)

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

how to loop a sequence in reverse?

A

for i in reversed(range(1,10):

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

how to loop a sequence in sorted order?

A

for i in sorted(set([‘apple’,’orange’]))

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