티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

What is the difference between Python's list methods append and extend?

Python List의 append()와 extend()의 차이점

 문제 내용 

What's the difference between the list methods append() and extend()?

List 함수 append()와 extend()의 차이점은 무엇입니까?

 

 

 

 높은 점수를 받은 Solution 

append appends a specified object at the end of the list:

append는 지정한 개체를 목록 끝에 추가합니다.
>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]

extend extends the list by appending elements from the specified iterable:

extend는 지정된 테이블의 요소를 추가하여 목록을 확장합니다.
>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]

 

 

 가장 최근 달린 Solution 

An English dictionary defines the words append and extend as:

영어 사전은 다음과 같이 append 및 expend 단어를 정의한다.

 

append: add (something) to the end of a written document.
extend: make larger. Enlarge or expand

추가: 문서의 끝에 추가합니다.
확장: 더 크게 만듭니다. 확대 또는 확장

 


With that knowledge, now let's understand

그 지식으로, 이제 이해합시다.

 

1) The difference between append and extend

1) 추가와 확장의 차이

 

append:

추가:
  • Appends any Python object as-is to the end of the list (i.e. as a the last element in the list).
  • The resulting list may be nested and contain heterogeneous elements (i.e. list, string, tuple, dictionary, set, etc.)
모든 Python 객체를 그대로 목록 끝에 추가합니다(예: 목록의 마지막 요소).
결과 List는 중첩될 수 있으며 다른 요소(예: 목록, 문자열, 튜플, 사전, 집합 등)를 포함할 수 있다.

 

extend:

확장:
  • Accepts any iterable as its argument and makes the list larger.
  • The resulting list is always one-dimensional list (i.e. no nesting) and it may contain heterogeneous elements in it (e.g. characters, integers, float) as a result of applying list(iterable).
허용 가능한 모든 변수를 인수로 사용하고 목록을 더 크게 만듭니다.
결과 List는 항상 1차원 목록(즉, 중첩 없음)이며 목록을 적용한 결과(itable)로 인해 해당 목록에 다른 요소(예: 문자, 정수, 플로트)를 포함할 수 있다.

 

2) Similarity between append and extend

2) 추가와 확장의 유사성

 

  • Both take exactly one argument.
  • Both modify the list in-place.
  • As a result, both returns None.
둘 다 정확히 하나의 인수를 받습니다.
둘 다 내부 List를 수정합니다.
결과적으로 둘 다 None을 리턴합니다.

 


Example

lis = [1, 2, 3]

# 'extend' is equivalent to this
lis = lis + list(iterable)

# 'append' simply appends its argument as the last element to the list
# as long as the argument is a valid Python object
list.append(object)

 

 

출처 : https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend

반응형
댓글
공지사항
최근에 올라온 글