프로그래밍언어/Python
[Python] 그림으로 이해하는 파이썬 List
문스코딩
2023. 8. 15. 23:41
List 선언
animal = [”ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
길이 얻기 (len)
animal = [”ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
print(len(animal)) // 7
Element 얻기
animal = [”ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
print(animal[3]) # "dog"
Index 얻기 (index)
animal = [”ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
print(animal.index("dog")) # 3
앞에 Element 추가/제거 (insert, del)
animal = [”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal.insert(0, "ant")
print(animal) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal = ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
del animal[0]
print(animal) # [”beer”, “cat”, ”deer”, “eagle”, “fox”, “giraffe”]
뒤에 Element 추가/제거 (append, pop)
animal = ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”]
animal.append(0, "giraffe")
print(animal) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal = ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal.pop()
print(animal) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”]
N번째에 Element 추가/제거 (insert, remove, pop)
animal = ["ant", ”beer”, “cat”, “eagle”, “fox”, “giraffe”]
animal.insert(3, "dog")
print(animal) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal = ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal.pop(3) # animal.remove("dog")
print(animal) # ["ant", ”beer”, “cat”, “eagle”, “fox”, “giraffe”]
Element 범위 얻기 ([n:m])
animal = ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
print(animal[3:]) # [”dog”, “eagle”, “fox”, “giraffe”]
print(animal[:4]) # ["ant", ”beer”, “cat”, "dog"]
print(animal[3:6]) # [”dog”, “eagle”, “fox”]
Element 변경하기 (1)
animal = ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, “giraffe”]
animal[3] = "deer"
print(animal) # ["ant", ”beer”, “cat”, ”deer”, “eagle”, “fox”, “giraffe”]
Element 변경하기 (2) - 범위
animal = ["ant", ”beer”, “cat”, ”apple”, “banana”, “cherry”, “fox”]
animal[3:6] = ["dog", "eagle"]
print(animal) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”]
합치기 (extend, +)
animalGroupA = ["ant", ”beer”, “cat”, ”dog”]
animalGroupB = ["eagle", ”fox”, “giraffe”]
animalGroupA.extend(animalGroupB)
print(animalGroupA) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, "giraffe"]
animalGroupA = ["ant", ”beer”, “cat”, ”dog”]
animalGroupB = ["eagle", ”fox”, “giraffe”]
animalGroupC = animalGroupA + animalGroupB
print(animalGroupC) # ["ant", ”beer”, “cat”, ”dog”, “eagle”, “fox”, "giraffe"]
초기화 (clear)
animal = ["ant", ”beer”, “cat”, ”dog”, "eagle", ”fox”, “giraffe”]
animal.clear()
print(animal) # []
반응형