언어/Python

List Comprehensions in Python

★ ☆ 2021. 1. 1. 00:20

List가 5의 배수를 하나라도 가지면 True를 리턴하고, 그렇지 않으면 False를 리턴한다.

def multiple_of_five(nums):
	"""Return True only when nums has a multiple of 5.
    """
    for num in nums:
    	if num % 5 == 0 and len(nums) != 0:
        	return True

List Comprehension으로 아래와 같이 단순화 할 수 있다.

def multiple_of_five(nums):
	"""Return True only when nums has a multiple of 5.
    """
	return any([num % 5 == 0 for num in nums])