-
[List] 자주쓰이는 리스트 메소드 정리🐍 파이썬/파이썬 기본 문법 2022. 7. 19. 06:27
1. concatenate
# 1. use + operation a = [1,2,3] b = [4,5] c = a+b # [1,2,3,4,5] # 2. use .append method x = [1,2,3] y = [4,5] x.append(y) # [1,2,3,[4,5]] # 3. use .extend method x = [1,2,3] y = [4,5] x.extend(y) # [1,2,3,4,5]
2. delete the value
l = [1,2,3,4,5] # 1. using pop() l.pop() # return 5, l=[1,2,3,4] # 2. delete by index using .del method del(l[0]) #return None, l=[2,3,4,5] # 3. delete by value using .remove method l.remove(3) # return None, l=[1,2,4,5]
3. sort
l = [3,7,-1,2] # sort in place l.sort() print(l) # [-1,2,3,7] # create new ordered array a = sorted(l) print(l) # [3,7,-1,2] print(a) # [-1,2,3,7] #sort reverse order l.reverse() print(l) # [2,-1,7,3]
'🐍 파이썬 > 파이썬 기본 문법' 카테고리의 다른 글
[OOP] class 클래스 개념 및 구성요소 (0) 2022.07.27 [list] reference and copy (aliasing and clone) (0) 2022.07.19 [concept] list vs tuple (0) 2022.07.19 [Variable scope] global scope vs function scope (0) 2022.07.18 what is first class object? (0) 2022.07.18