List
list: A mutable sequence* that grows or shrinks dynamically as new elements are added or removed.
*sequence: A container that stores a collection of values that can be accessed by an integer index. (string, list, …)
Use SQUARE BRACKETS [ ] to create a list.
ex) values = [32, 54, 67.5, 80, 110, 44.5] # A list with 6 elements
Another way to create a list:
values = [4]
sets values to the list [4]; that is, the list containing the single element 4.
Can access and change elements with the subscript operator ([ ])
Lists can hold values of any type, and mutable. (Can replace element)
ex) values[2] = 30.5 # Replace third element to 30.5
Attempting to access an element whose index is not within the valid index range is called an out-of-range error* or a bounds error**.
*out-of-range error: Attempting to access an element whose index is not in the valid index range; bounds error.
**bounds error: Trying to access a sequence element that is outside the legal range.
Traversing Lists
There are two common ways of visiting all elements of a list.
1. If you need the index values
for i in range(len(values)) :
print(i, values[i])
2. If you don’t need the index values
for element in values :
print(element)
List References
In Python, a list is stored elsewhere and the list variable hold a reference* to the list.
*reference: A value that denotes the location of an object in memory. In Python, a variable whose type is a class contains a reference to an object of that class.
When you copy a list variable into another, both variables refer to the same list.
The second variable is an alias for the first because they reference the same list.
scores = [10, 9, 8, 7]
values = scores # Copying list reference
Thus, if you change the element of alias, the original variable also gets affected.
List Operations
Appending Elements
Use .append method
ex) friends = [ ]
friends.append(“Harry”)
friends.append(“Bob”)
…
Inserting Elements
Use .insert method
ex) friends = [“Harry”, “Emily”, “Bob”, “Cari”]
friends.insert(1, “Cindy”)
# “Cindy” is inserted at index 1, and all of the elements following position 1
are moved down by one position.
print(friends) # [“Harry”, “Cindy”, “Emily”, “Bob”, “Cari”]
Finding Elements
If you simply want to know whether an element is present in a list, use in operator
ex) if “Cindy” in friends:
print(“She’s a friend”)
If you want to know the index, use .index method
ex) friends = [“Harry”, “Cindy”, “Emily”, “Mike”, “Cindy”]
n = friends.index(“Cindy”) # n = 1
There might be more than one value, then you can specify the starting index
ex) n2 = friends.index(“Cindy”, n + 1) # n = 4, searched from index 2
application:
if “Cindy” in friends:
n = friends.index(“Cindy”)
else:
n = -1
Removing Elements
The .pop method removes the element at a given position.
It also returns the removed value.
If used without an argument, it removes the last element.
ex) friends = [“Cindy”, “Micah”, “Ricky”]
print(friends.pop(1))
# Prints “Micah” and removes it from the list friends
The .remove method removes an element by value instead of position.
ex) if “Ricky” in friends:
friends.remove(“Ricky”)
Concatenation and Replication
The concatenation can be done by plus (+) operator.
The replication can be done by replication (*) operator.
ex) myFriends = [“Mike”, “Julie”]
yourFriends = [“Sylas”, “Amy”]
ourFriends = myFriends + yourFriends # [“Mike”, “Julie”, “Sylas”, “Amy”]
monthlyScores = [0] * 12 # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Sum, Min, Max, and Sorting
You can use sum, min, max, and .sort( ) to a list.
ex) myList = [1, 9, 4, 16]
print(sum(myList)) # 30
print(max(myList)) # 16
print(min(myList)) # 1
myList.sort() # myList = [1, 4, 9, 16]
Copying List
If you copy the reference, you get another reference to the same list.
ex) prices = values
If you want to make a copy of a list; that is, a new list that has the same elements in the same order as a given list.
Use the list function:
ex) values = [10, 20, 30]
prices = list(values)
# Now, values and prices refer to different lists
# You can modify either without affecting the other.
When list function is used to a string:
ex) characters = list(“Hello”) # characters = [“H”, “e”, “l”, “l”, “o”]
Slicing Lists
If you are interested in specific index of values, you can use Python’s slice operator (:) to obtain them.
ex) temperatures = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
print(temperatures[2: 5]) # [8, 7, 6]
print(temperatures[5:]) # [5, 4, 3, 2, 1]
print(temperatures[:3]) # [10, 9, 8]
'python' 카테고리의 다른 글
Tuple 튜플 (0) | 2025.01.26 |
---|---|
Terminating a Program (프로그램 종료) (0) | 2025.01.12 |
Analyzing String | 문자열 함수들 (0) | 2025.01.12 |
[baekjoon] 2563번 색종이 Python (0) | 2023.10.14 |
[baekjoon] 10789번 세로읽기 Python (1) | 2023.10.14 |