Operating on Sequence Types

We can iterate over the items in a sequence s in a variety of useful ways: 

Table : Various ways to iterate over sequences
Python ExpressionComment
for item in siterate over the items of s
for item in sorted(s)iterate over the items of s in order
for item in set(s)iterate over unique elements of s
for item in reversed(s)iterate over elements of s in reverse
for item in set(s).difference(t)iterate over elements of s not in t
for item in random.shuffle(s)iterate over elements of s in random order


Popular Posts