List append vs extend

Chalsu Chalsu·2021년 12월 25일
0

Python Issue

목록 보기
3/4

List에 Not String element를 추가

  • (+)
  • append
  • extend
test = [1,2]

# 올바른 예시
test += [3] # [1,2,3]
test.extend([3]) # [1,2,3]
test.append(3) # [1,2,3]

# 틀린 예시
test += 3 # TypeError: 'int' object is not iterable
test.extend(3) # TypeError: 'int' object is not iterable
test.append([3]) # [1,2,[3]]

List에 String element를 추가

  • List에 String을 추가할 때 주의할 점 존재
test = ["Lee","Jae"]

test.extend(["Cheol"]) # ["Lee","Jae","Cheol"]
test.append("Cheol") # ["Lee","Jae","Cheol"]

test.extend("Cheol") # ["Lee", "Jae", "C", "h", "e", "o", "l"]
test.append(["Cheol"]) # ["Lee", "Jae", ["Cheol"]]
profile
https://github.com/MrLee5693

0개의 댓글