List
mylist = ['apple', 'banana', 'orange', 'apple']
print(list)
> ['apple', 'banana', 'orange', 'apple']
print(len(mylist))
> 3
List Items - Data Types
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
list3 = [True, False]
list4 = ['a', 12, True]
print(type(list4))
> <class 'list'>
The list() Constructor
mylist = list(('a', 'b', 'c'))
print(mylist)
> ['a', 'b', 'c']
Access Items
print(mylist[1])
> b
print(mylist[-1])
> c
- list slicing
- positive indexes -1
- negative indexes -2
list1 = ['a', 'ab', 'apple', 'orange', 'kiwi', 'lemon']
print(list1[2:5])
> ['apple', 'orange', 'kiwi']
print(list1[:4])
> ['a', 'ab', 'apple', 'orange']
print(list1[2:])
> ['apple', 'orange', 'kiwi', 'lemon']
list1 = ['a', 'ab', 'apple', 'orange', 'kiwi', 'lemon']
print(list1[-4: -1])
> ['apple', 'orange', 'kiwi']
Check if Item Exists
if 'lemon' in list1:
print('yes')
> yes
Change List Items
mylist = ['apple', 'banana', 'orange']
mylist[1] = 'blueberry'
print(mylist)
> ['apple', 'blueberry', 'orange']
mylist[1:2] = ['banana', 'lemon']
print(mylist)
> ['apple', 'banana', 'lemon', 'orange']
thelist = ['apple', 'banana', 'orange', 'kiwi', 'lemon']
thelist[1:3] = ['wow', 'gum']
print(thelist)
> ['apple', 'wow', 'gum', 'kiwi', 'lemon']
Add List Items
- append()
- insert()
- extend()
list1 = ['a', 'b', 'c']
list1.append('d')
print(list1)
> ['a', 'b', 'c', 'd']
list1.insert(1, 'lemon')
print(list1)
> ['a', 'lemon', 'b', 'c', 'd']
list2 = ['berry', 'apple', 'melon']
list1.extend(list2)
print(list1)
> ['a', 'lemon', 'b', 'c', 'd', 'berry', 'apple', 'melon']
Remove List Items
- remove()
- pop()
- 삭제할 index 지정
- index 지정 x → last index 값 삭제
- stack 형태
- del
- 특정 index 지정
- index 지정 x → list 자체를 delete
- clear()
- list 내부의 값들을 clear
- list는 남아 있음!! 내용만 없어짐. # del 차이점
list1.remove('d')
print(list1)
> ['a', 'lemon', 'b', 'c', 'berry', 'apple', 'melon']
list1.pop(1)
print(list1)
> ['a', 'b', 'c', 'berry', 'apple', 'melon']
list1.pop()
print(list1)
> ['a', 'b', 'c', 'berry', 'apple']
del list1[0]
print(list1)
> ['b', 'c', 'berry', 'apple']
del list1
print(list1)
list1 = ['a', 'b']
list1.clear()
print(list1)
> []
List Comprehension
fruits = ['apple', 'banana', 'kiwi', 'orange', 'melon']
newlist = []
for x in fruits:
if 'a' in x:
newlist.append(x)
print(newlist)
> ['apple', 'banana', 'orange']
fruits = ['apple', 'banana', 'kiwi', 'orange', 'melon']
newlist = [x for x in fruits if 'a' in x]
print(newlist)
> ['apple', 'banana', 'orange']
List = [x for x in fruits if x != 'apple']
print(List)
> ['banana', 'kiwi', 'orange', 'melon']
list1 = [x for x in range(10)]
print(list1)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sort Lists
fruits = ['apple', 'kiwi', 'orange', 'melon', 'banana']
fruits.sort()
print(fruits)
> ['apple', 'banana', 'kiwi', 'melon', 'orange']
fruits = ['apple', 'kiwi', 'orange', 'melon', 'banana']
fruits.sort(reverse=True)
print(fruits)
> ['orange', 'melon', 'kiwi', 'banana', 'apple']
Copy Lists
fruits = ['apple', 'kiwi', 'orange', 'melon', 'banana']
list1 = fruits.copy()
print(list1)
> ['apple', 'kiwi', 'orange', 'melon', 'banana']
Join Two Lists
fruits = ['apple', 'kiwi', 'orange', 'melon', 'banana']
list2 = [1, 2, 3]
list3 = list2 + fruits
print(list3)
> [1, 2, 3, 'apple', 'kiwi', 'orange', 'melon', 'banana']
Tuple
- list와 굉장히 유사
- list와의 차이점
- 소괄호를 사용함 → list: [], tuple: ()
- change-up X → 수정이 안 됨!!!
- 공통점
- container형(box형) 변수
- index로 특정 요소 access
- iterable → for 문으로 돌릴 수 있음
- 장점
- iteration (for문을 돌릴 때) 속도가 list보다 훨씬 빠름
mytuple = ['apple', 'banana', 'orange']
mytuple[1] = 'abc'