👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
list = [123]
list.extend(4);
 
 

 

 

🖨️오류

TypeError: 'int' object is not iterable

 

 

📡 원인

list에서 extend는 iterable한 객체를 추가하고자 할 때 사용하므로 정수를 iterable한 객체로 사용하려고 해서 오류가 발생

* iterable한 객체: 순회 가능한 객체, 리스트, 튜플, 문자열과 같은 컨테이너 타입

 

 

📰 해결 방법

1
2
3
list = [123]
list.extend("4");
 
 

추가하려는 요소를 iterable한 객체로 변경

 

1
2
3
list = [123];
list.append(4);
 
 

모든 요소를 추가할 수 있는 append() 사용

 

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

Data Structures : List

Main Methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
list1 = ["a""b""c"]
list1.append("d")
# Add an item to the end of the list
print(list1)
# output: ['a', 'b', 'c', 'd']
 
 
list2 = [123]
# list2.extend(4);
# Extend the list by appending all the items from the iterable
# TypeError: 'int' object is not iterable
list2.extend("4")
print(list2)
# output: [1, 2, 3, '4']
 
 
list3 = ["ga""na""ra"]
list3.insert(2"da")
# Insert an item at a given position.
print(list3)
# output: ['ga', 'na', 'da', 'ra']
 
 
list4 = ["do""re""mi"]
list4.remove("re")
# Remove the first item from the list whose value is equal to x.
# list4.remove("pa");
# ValueError: list.remove(x): x not in list
print(list4)
# output: ['do', 'mi']
 
 
list5 = ["a""b""c"]
list5.pop(1)
# Remove the item at the given position in the list, and return it.
# If no index is specified, a.pop() removes and returns the last item in the list.
print(list5)
# output: ['a', 'b']
 
 
list6 = [123]
list6.clear()
# Remove all items from the list.
print(list6)
# output: []
 
 
list7 = ["ga""na""ra""na""da""ma""ba""na"]
index1 = list7.index("na")
# Return zero-based index in the list of the first item whose value is equal to x.
print(index1)
# output: 1
index2 = list7.index("na"2)
# The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list.
print(index2)
# output: 3
# index3 = list7.index("na", 5, 6);
# print(index3);
# ValueError: 'na' is not in list
 
 
list8 = ["do""re""mi""mi"]
mi_cnt = list8.count("mi")
# Return the number of times x appears in the list.
print(mi_cnt)
# output: 2
 
 
list9 = ["1""4""3""5""2"]
list9.sort()
# Sort the items of the list in place.
print(list9)
# output: ['1', '2', '3', '4', '5']
list9.sort(reverse=True)
print(list9)
# output: ['5', '4', '3', '2', '1']
 
 
list10 = ["1""4""3""5""2"]
list10.reverse()
# Reverse the elements of the list in place.
print(list10)
# output: ['2', '5', '3', '4', '1']
 
 
list11 = ["apple""banana""car""door""elephant"];
list12 = list11.copy();
# Return a shallow copy of the list.
print(list12);
# output: ['apple', 'banana', 'car', 'door', 'elephant']
 
 
 

 

 

 

📚 참고 자료

 

5. Data Structures

This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. More on Lists: The list data type has some more methods. Here are all of the method...

docs.python.org

 

'Python > Python' 카테고리의 다른 글

[Python_Bootcamp] 리스트 랜덤 추출  (0) 2023.08.16
[Python_Bootcamp] f-String  (0) 2023.08.08

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Import the random module here
import random;
 
# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
# 🚨 Don't change the code above 👆
 
#Write your code below this line 👇
print(names);
random_num = random.randint(0len(names));
print(names[random_num] + " is going to buy the meal today!")
IndexError: list index out of range
 
 
 

 

 

🖨️오류

IndexError: list index out of range

 

 

📡 원인

list의 범위를 넘어서는 index 사용

예: list 길이가 5일 때, 인덱스 범위는 0~4이므로 인덱스로 5를 사용하면 IndexError 발생

⭐ 리스트 인덱스 시작값은 0

 

 

📰 해결 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Import the random module here
import random;
 
# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
# 🚨 Don't change the code above 👆
 
#Write your code below this line 👇
print(names);
random_num = random.randint(0len(names)-1);
print(names[random_num] + " is going to buy the meal today!")
IndexError: list index out of range
 
 
 

list 타입의 변수 names의 길이가 5일 경우, index가 갖을 수 있는 범위를 0~4로 설정해야 함

▶ randint 사용 시, 시작값을 0으로 할 경우에는 종료값은 list 길이 -1

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
names = ["Hello""World"];
random_num = random.randint(0, names.len());
 
 
 

 

 

🖨️오류

AttributeError: 'list' object has no attribute 'len'

 

 

📡 원인

list 타입의 names는 내장 메서드로 len()을 갖고 있지 않음

 

 

📰 해결 방법

1
2
3
names = ["Hello""World"];
random_num = random.randint(0len(names));
 
 
 

list 타입의 길이는 len()안에 list 타입의 변수를 매개변수로 전달

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
4
5
if height=120:
  print("Yes!");
else :
  print("No");
 
 

 

 

🖨️오류

SyntaxError: invalid syntax

 

 

📡 원인

if 조건문의 내용은 boolean 타입의 true, false로 반환될 수 있어야 함

 

 

📰 해결 방법

1
2
3
4
5
if height>=120:
  print("Yes!");
else :
  print("No");
 
 

true/false를 반환할 수 있도록 부등호 추가

 

1
2
3
4
5
if height==120:
  print("Yes!");
else :
  print("No");
 
 

true/false를 반환할 수 있도록 등호 추가