[Python] Important standard library_itertools
It is said to be a function that creates an iterator for efficient looping. Here, looping means looping, that is, repetition. An iterator is an object that contains several elements and allows you to take them out one by one and perform an operation. This module is used to conveniently create iterators. Based on the contents of 파이썬 표준라이브러리, it is as follows.
Infinite iterator types - count(), cycle(), repeat()
itertools.count( )
In the standard example, you can see that the count function of the itertools module has a start point and a step point, and the default values are 0 and 1, respectively. You can see that it starts with start, increases the value by the value of step, and repeats infinitely. Creating a value that continues to repeat without knowing the maximum value
+ Separately from this, among the basic string functions, there is also the use of the count() function, which allows you to know the number of characters.
itertools.cycle( )
In the standard example, the cycle function in the itertools module is a function that returns the elements of an iterable in order and repeatedly returns the iterable when it is exhausted.
itertools.repeat( )
In the standard example, the repeat function in the itertools module is a function that returns the number of elements of an object times.
Iterator type that terminates on the shortest input sequence
- accumulate(), chain(), chain.from_iterable(), dropwhile(), filterfalse(), islice(), starmap(), takewhile(), comporess()
itertools.accumulate( )
In the standard example, the accumulate function of the itertools module operates on iterable elements according to the initial value according to func, as follows. In the function, as shown in the example, addition operations such as add, min minimum value, max maximum value, and mul multiplication operation are possible. Since there is no inital default value here, you can proceed with the operation starting from the first value of the iterable element. If there is an inital value, that value is returned first and then the func operation is performed.
itertools.chain( )
Proceeds until all iterable elements are returned, and unlike repeat(), only elements are returned without repetition. If there are multiple iterables, they are returned sequentially.
itertools.chain.from_iterable()
An alternative constructor to chain(), it turns iterable elements into one continuous sequence. Since only one argument is used, a TypeError occurs if multiple arguments are presented.
itertools.comporess(data, selectors)
This is a function that returns only True elements according to selectors in the presented data.
itertools.dropwhile(predicate, iterable)
Filters out iterable elements whose predicate value is true. In other words, the condition is based on a value less than 5, and if the iterable value is 0 to 10, the output value is a number from 5 to 10.
itertools.filterfalse(predicate, iterable)
Returns the iterable element whose predicate value is true. In other words, if the condition says that a number divisible by 5 is true, 0 and 5 are returned from numbers 0 to 9. Note that 0 is also returned! When the value of iterable is set to range(10), the value represents a number from 0 to 9.
itertools.islice(iterable, start, stop, step)
It is an iterator that returns the selected element. In iterable elements, it is returned according to the step interval from the start element to the stop element. That is, in the value of range(10), if the start is 1, the end is 5, and the interval is 2, the returned values are 1 and 3.
itertools.starmap(function, iterable)
Iterable values are calculated according to the function. If the iterable is grouped into a tuple, each value is operated and returned according to the function.
itertools.takewhile()
Returns the element for which the predicate is true. At this time, duplicate elements in the iterable are not returned.
Combination iterators - product(), permutation(), combination(), combination_with_replacement ()
itertools.product() returns all possible combinations. (Duplicates allowed)
itertools.permutations() returns a permutation iterator.
itertools.combinations() returns a combination iterator.
itertools.combinations_with_replacement() also returns pairs of the same elements in the combination iterator.
파이썬 문법 6 - 이터레이터(Iterator) 와 제너레이터(Generator)
(파이썬) itertools 모듈의 count 함수 - 코딩 연습
Python - Itertools.chain.from_iterable() - GeeksforGeeks
R, Python 분석과 프로그래밍의 친구 (by R Friend) :: [Python] itertools를 활용한 리스트 원소를 n번 반복하여 새로운 리스트 만들기
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going