Python

[python] extend 함수

nang. 2020. 10. 8. 23:09
반응형
SMALL

append()와 다른점

  • append()
    • 리스트 자체를 붙여줌
x = ['a', 'b', 'c']
y = ['d', 'e', 'f']

x.append(y)
print(x)

>> ['a', 'b', 'c', ['d', 'e', 'f']]
  • extend()
    • iterable의 모든 하나하나의 항목을 붙여줌
      • 넣은 뒤에 마치 원래있던 하나의 리스트처럼
x = ['a', 'b', 'c']
y = ['d', 'e', 'f']

x.extend(y)
print(x)

>> ['a', 'b', 'c', 'd', 'e', 'f']

붙이려는게 문자열이면?

  • append()
x = ['a', 'b', 'c']
y = 'hi'

x.append(y)
print(x)

>> ['a', 'b', 'c', 'hi']
  • extend()
x = ['a', 'b', 'c']
y = 'hi'

x.extend(y)
print(x)

>> ['a', 'b', 'c','h', 'i']
반응형
LIST

'Python' 카테고리의 다른 글

[python] sort(), sorted() 함수  (0) 2020.10.09
[python] reverse(), reversed() 함수  (0) 2020.10.08
[python] lambda 함수  (0) 2020.10.08
[python] enumerate 함수  (0) 2020.10.08
[python] _ (언더스코어)  (0) 2020.10.08