List

  • 선언 방법 : 대괄호를 이용해 선언해준다.
a = 10
b = 20
c = 30

d = [10, 20, 30]

위와 같이 여러 변수를 사용하는 것 보다 하나의 리스트를 이용해 여러 값들을 관리할 수 있다. 리스트에선 index 로 관리할 수 있다.

  • append : 리스트에 값을 추가한다.(가장 마지막 index에 추가됨)
  • insert : 지정한 index에 값을 추가한다.
  • pop : 가장 마지막 index 값을 꺼냄
  • index : 지정한 값의 index값을 가져옴
  • count : 지정한 값이 리스트에 몇 개 들어있는 지 확인
  • clear : 모두 지우기
station = ["강남역", "신논현역","역삼역"]

station.append("선릉역")
print(station)
print(station.index("역삼역"))

station.insert(1,"교대역")
print(station)

print(station.pop())

station.append("강남역")
print(station.count("강남역"))

print(station.clear())

#['강남역', '신논현역', '역삼역', '선릉역']
#2
#['강남역', '교대역', '신논현역', '역삼역', '선릉역']
#선릉역
#2
#None

정렬

  • sort : 작은 수부터 큰 수로 정렬
  • reverse : 순서 뒤집기
num = [5,4,3,2,1]
num_list.sort()
print(num_list)

num_list.reverse()
print(num_list)

여러 자료형 함께 사용

mixList = ["python",10,True]
numList = [5,4,3,2,1]

numList.extend(mixList)
print(numList)

#[5, 4, 3, 2, 1, 'python', 10, True]

+ Recent posts