본문 바로가기
프로그래밍/Python 관련 정보

[Python 기초] all(), any()

by TrillionNT 2025. 4. 26.

2025.03.15 - [프로그래밍/Python 관련 정보] - [Python] Table of Contents


1. all()

  • iterable(리스트, 튜플, 세트 등) 안의 모든 요소가 **참(True)**이면 True를 반환합니다. 하나라도 False가 있으면 False를 반환합니다.
  • 빈 iterable에 대해서는 기본적으로 True를 반환합니다.

2. any()

  • iterable 안에 **하나라도 참(True)**인 요소가 있으면 True를 반환합니다. 전부 False이면 False를 반환합니다.
  • 빈 iterable에 대해서는 기본적으로 False를 반환합니다.

 

이 함수를 응용해볼 수 있는 문제를 하나 풀어볼까요?

https://leetcode.com/problems/patients-with-a-condition/?envType=study-plan-v2&envId=30-days-of-pandas&lang=pythondata

 

import pandas as pd

def find_patients(patients: pd.DataFrame) -> pd.DataFrame:
    return patients[patients.conditions.apply(lambda x: any([y.startswith('DIAB1') for y in x.split()]))]