DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Iterator functions in Python

Buy Me a Coffee

*Memo:

  • My post explains an iterator (1).

enumerate() can return the iterater which creates pairs(tuples) of the number incremented by 1 and the element of iterable one by one as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable).
  • The 2nd argument is start(Optional-Default:0-Type:int).
fruits = ['Apple', 'Orange', 'Banana', 'Kiwi']

v = enumerate(iterable=fruits)

print(v)
# <enumerate object at 0x0000024458E998F0>

print(next(v)) # (0, 'Apple')
print(next(v)) # (1, 'Orange')
print(next(v)) # (2, 'Banana')
print(next(v)) # (3, 'Kiwi')
print(next(v)) # StopIteration: 
Enter fullscreen mode Exit fullscreen mode
fruits = ['Apple', 'Orange', 'Banana', 'Kiwi']

v = enumerate(iterable=fruits, start=5)

print(v)
# <enumerate object at 0x0000024458E998F0>

print(next(v)) # (5, 'Apple')
print(next(v)) # (6, 'Orange')
print(next(v)) # (7, 'Banana')
print(next(v)) # (8, 'Kiwi')
print(next(v)) # StopIteration: 
Enter fullscreen mode Exit fullscreen mode

zip() can return the iterator which aggregates one element from each of *iterables to create tuples of them one by one as shown below:

*Memo:

  • The 1st arguments are *iterables(Optional-Default:()-Type:Iterable):
    • Don't use any keywords like *iterables=, iterables=, etc.
  • It stops if all the elements of the shortest input iterable are exhausted.
fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Lemon', 'Mango']
meats = ['Chicken', 'Beef', 'Pork', 'Duck']
vegetables = ['Onion', 'Carrot', 'Garlic', 'Spinach', 'Eggplant']

print(zip())
# <zip object at 0x7eace0bead00>

v = zip(fruits, meats, vegetables)

print(v)
# <zip object at 0x7da5876aaa40>

print(next(v)) # ('Apple', 'Chicken', 'Onion')
print(next(v)) # ('Orange', 'Beef', 'Carrot')
print(next(v)) # ('Banana', 'Pork', 'Garlic')
print(next(v)) # ('Kiwi', 'Duck', 'Spinach')
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode

sorted() can convert an iterator to a list, then sort the list, then the sorted list is converted to an iterator with iter() as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
  • The 2nd argument is key(Optional-Default:None-Type:Callable or NoneType).
  • The 3rd argument is reverse(Optional-Default:False-Type:bool) to reverse the list.
  • sorted() creates a copy:
    • Be careful, sorted() does shallow copy instead of deep copy as my issue.
import copy

v1 = iter([3, 5, -2, 1, -4])

v2 = sorted(copy.copy(v1))
v2 = sorted(copy.copy(v1), key=None, reverse=False)

print(v2)
# [-4, -2, 1, 3, 5]

v2 = iter(sorted(copy.copy(v1)))

print(next(v2), next(v2), next(v2), next(v2), next(v2))
# -4 -2 1 3 5

v2 = iter(sorted(copy.copy(v1), reverse=True))

print(next(v2), next(v2), next(v2), next(v2), next(v2))
# 5 3 1 -2 -4

v2 = iter(sorted(copy.copy(v1), key=abs))

print(next(v2), next(v2), next(v2), next(v2), next(v2))
# 1 -2 3 -4 5

v2 = iter(sorted(copy.copy(v1), key=abs, reverse=True))

print(next(v2), next(v2), next(v2), next(v2), next(v2))
# 5 -4 3 -2 1
Enter fullscreen mode Exit fullscreen mode
v1 = ("apple", "Banana", "Kiwi", "cherry")

""" Case sensitive sort """
v2 = iter(sorted(v1))

print(next(v2), next(v2), next(v2), next(v2))
# Banana Kiwi apple cherry

""" Case insensitive sort """
v2 = iter(sorted(v1, key=str.upper))
v2 = iter(sorted(v1, key=str.lower))

print(next(v2), next(v2), next(v2), next(v2))
# apple Banana cherry Kiwi

""" Sort by the length of a word """
v2 = iter(sorted(v1, key=len))

print(next(v2), next(v2), next(v2), next(v2))
# Kiwi apple Banana cherry
Enter fullscreen mode Exit fullscreen mode

Top comments (0)