반응형
LIST.extend()
extend()는 리스트뿐 아니라 튜플, 집합(set) 등 다양한 자료형의 요소를
개별적으로 풀어서 리스트에 순서대로 덧붙이는 기능을 제공합니다.
extend() 특징
- 간결한 코드 작성
- 여러 요소를 추가할 때 for 루프나 append()를 반복적으로 호출할 필요가 없습니다.
- 다양한 자료형에 적용 가능
- 리스트뿐 아니라 튜플, 집합, 문자열 등 다양한 자료형의 요소를 추가할 수 있습니다.
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
# extend()를 사용하여 tropical의 요소를 thislist에 추가
thislist.extend(tropical)
print(thislist)
# ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
extend() vs append() 비교
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
# append() 사용 시
thislist.append(tropical)
print("append 사용 결과:", thislist)
# extend() 사용 시
thislist = ["apple", "banana", "cherry"] # 초기화
thislist.extend(tropical)
print("extend 사용 결과:", thislist)
# append 사용 결과: ['apple', 'banana', 'cherry', ['mango', 'pineapple', 'papaya']]
# extend 사용 결과: ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
- append()는 리스트 자체를 마지막 요소로 추가하여 리스트 안에 리스트가 들어가는 형태를 만듭니다.
- extend()는 각 요소를 개별적으로 추가해 리스트가 중첩되지 않으므로 코드의 가독성과 자료 접근성이 향상됩니다.
728x90
반응형
'Develop' 카테고리의 다른 글
[React] dyld: Library not loaded: /usr/local/homebrew/opt/icu4c/lib/libicui18n.74.dylib (0) | 2024.12.10 |
---|---|
[Python] except 상세 logging 방법 (1) | 2024.11.27 |
[Python] SyntaxError: f-string: unmatched '[' 원인 및 해결 방법 (0) | 2024.11.26 |
[Docker] docker-compose 실행 및 중지, 테스트 방법 (0) | 2024.11.25 |
[Python] BeautifulSoup select() CSS 선택자 사용법 (2) | 2024.11.17 |
[Web] Rendering on the Web (0) | 2024.11.12 |
[OpenAI] GPT Response JSON Schema로 관리하기 (5) | 2024.11.07 |
[OpenAI] 1.0.0 Migration 관련 Function 변경 (2) | 2024.11.05 |