Python에서 목록을 사용하려면 먼저 목록을 변수에 할당하여 목록을 만들어야 합니다.
그런 다음
.append() 메서드를 사용하여 목록에 항목을 추가하거나
.insert() 메서드를 사용하여 특정 위치에 항목을 삽입할 수 있습니다. 또한 목록의 인덱스를 사용하여 개별 요소에 액세스하고
.remove() 메서드를 사용하여 항목을 삭제하고
.sort() 메서드를 사용하여 목록을 정렬할 수 있습니다.
코드 예시
# Create an empty list
my_list = []
# Add items to the list
my_list.append(1)
my_list.append(2)
my_list.append(3)
# Insert an item in a specific position
my_list.insert(1, 0)
# Access individual elements
print(my_list[2]) # Output: 2
# Delete an item
my_list.remove(2)
# Sort the list
my_list.sort()
# Print the list
print(my_list) # Output: [0, 1, 3]
반응형
'기타' 카테고리의 다른 글
[Python] set (0) | 2023.01.27 |
---|---|
[Python] tuple (0) | 2023.01.27 |
[Python] 변수와 함수 (0) | 2023.01.27 |
R프로그램과 파이썬의 비교 (0) | 2023.01.24 |
[Python] asterisk (0) | 2023.01.17 |