# 4.10.1.     리스트(Lists)

리스트는 대괄호 사이에 쉼표로 구분된 값(item)들의 목록으로 작성될 수 있는 파이썬에서 가장 다재 다능한 데이터 유형입니다. 리스트는 목록이라는 뜻으로, 다양한 데이터를 담을 수 있고 내용을 변경할 수 있는 시퀀스입니다. 리스트의 중요한 점은 리스트의 항목이 동일한 유형 일 필요는 없다는 것입니다. 리스트의 인덱스는 0부터 시작합니다.

리스트를 만들 때는 위에서 보는 것과 같이 대괄호(\[ ])로 감싸 주고 각 항목 값들은 쉼표(,)로 구분해 줍니다.

```python
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
list4 = [1, 2, 'Life', 'is'] 
list5 = [1, 2, ['Life', 'is']]
```

리스트의 각 요소들을 액세스하려면 대괄호를 사용하여 색인과 함께 슬라이싱하고 해당 색인에 있는 값을 구하면 됩니다. (\* 슬라이싱: 범위를 정해 선택하기)

```python
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
```

대입 연산자 = 의 왼쪽에 리스트\[인덱스] 를 제공하여 목록의 단일 또는 여러 요소를 업데이트 할 수 있으며 append() 메서드를 사용하여 목록의 요소에 추가할 수 있습니다. 예를 들어 -

```python
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]
```

목록 요소를 제거하려면 삭제할 요소를 정확히 알고 있는 경우 del 문을 사용하고, 모르는 경우 remove() 메서드를 사용할 수 있습니다. 예를 들어 -

```python
list1 = ['physics', 'chemistry', 1997, 2000];
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1
```

리스트는 문자열과 매우 비슷하게 +와 \* 연산자를 사용합니다. 연산의 결과가 새로운 목록이지 문자열이 아니라는 것을 제외하고 + 연산자는 연결,  \* 연산자는 반복을 의미합니다.

| **Python Expression**         | **Results**                   | **Description** |
| ----------------------------- | ----------------------------- | --------------- |
| len(\[1, 2, 3])               | 3                             | Length          |
| \[1, 2, 3] + \[4, 5, 6]       | \[1, 2, 3, 4, 5, 6]           | Concatenation   |
| \['Hi!'] \* 4                 | \['Hi!', 'Hi!', 'Hi!', 'Hi!'] | Repetition      |
| 3 in \[1, 2, 3]               | True                          | Membership      |
| for x in \[1, 2, 3]: print x, | 1 2 3                         | Iteration       |

리스트는 시퀀스이므로 문자열과 동일한 방식으로 인덱싱과 슬라이싱 동작을 합니다. 파이썬에는 다음과 같은 리스트 함수와 메서드들이 있습니다.

| [**cmp(list1, list2)**](https://www.tutorialspoint.com/python/list_cmp.htm) 두 리스트의 요소를 비교합니다.                             |
| ------------------------------------------------------------------------------------------------------------------------- |
| [**len(list)**](https://www.tutorialspoint.com/python/list_len.htm)  리스트의 전체 길이                                           |
| [**max(list)**](https://www.tutorialspoint.com/python/list_max.htm) 리스트에서 최대 값을 가진 항목을 반환                                 |
| [**min(list)**](https://www.tutorialspoint.com/python/list_min.htm)  리스트에서 최대 값을 가진 항목을 반환                                |
| [**list(seq)**](https://www.tutorialspoint.com/python/list_list.htm)  튜플을 리스트로으로 변환                                       |
| [**list.append(obj)**](https://www.tutorialspoint.com/python/list_append.htm)  obj 오브젝트를리스트에 추가                           |
| [**list.count(obj)**](https://www.tutorialspoint.com/python/list_count.htm) 리스트에 obj가 발생한 횟수를 반환                          |
| [**list.extend(seq)**](https://www.tutorialspoint.com/python/list_extend.htm)  리스트에 seq의 내용을 추가                           |
| [**list.index(obj)**](https://www.tutorialspoint.com/python/list_index.htm) obj가 나타나는 리스트 내의 가장 작은 인덱스를 리턴                |
| [**list.insert(index, obj)**](https://www.tutorialspoint.com/python/list_insert.htm) 리스트에 객체 obj를 오프셋 index 위치에 삽입        |
| [**list.pop(obj=list\[-1\])**](https://www.tutorialspoint.com/python/list_pop.htm) 리스트에서 마지막 객체 또는 obj를 제거하여 반환.          |
| [**list.remove(obj)**](https://www.tutorialspoint.com/python/list_remove.htm)  오브젝트 obj를 리스트로부터 삭제                        |
| [**list.reverse()**](https://www.tutorialspoint.com/python/list_reverse.htm)  리스트의 대상을 반전                                 |
| [**list.sort(\[func\])**](https://www.tutorialspoint.com/python/list_sort.htm) 리스트의 객체를 정렬하고, 주어진 compare \[func]를 사용합니다. |
