본문 바로가기
Python

[Python] List (Array) Data Structure

by llHoYall 2020. 12. 7.

List is a data structure like Array.

Elements in the List can be duplicated.

Creating List

### [] method
emptyList = []
weekdayList = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
nameList = ['Kim', 'Lee', 'Park', 'Kim']


### list() function
emptyList = list()

testList = list('cat')  # from String
print(testList)
# ['c', 'a', 't']

testTuple = ('red', 'green', 'blue')
testList = list(testTuple)  # from Tuple
print(testList)
# ['red', 'green', 'blue']


### List Comprehension
numberList = [number for number in range(1, 6)]
print(numberList)
# [1, 2, 3, 4, 5]

numberList = [number for number in range(1, 6) if number % 2 == 1]
print(numberList)
# [1, 3, 5]

rows = range(1, 4)
cols = range(1, 3)
cells = [(row, col) for row in rows for col in cols]
print(cells)
# [(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2)]

Getting Element from List

weekdayList = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']


### [index]
print(weekdayList[0])
# Mon


### pop() method
print(weekdayList.pop())
# Fri

print(weekdayList.pop(1))
# Tue

If you call pop() method without argument, it returns the last element.

If you call pop() method with an argument, it returns the indexed element.

Slicing

Slicing uses [] operator.

[start index:end index:step]

It gets an element by skipping step from the start index to the end index.

It doesn't include an element of the end index.

The step can be omitted.

If you omit the start index, it points to the first element, and if you omit the end index, it points to the last element.

testList = ['A', 'B', 'C', 'D', 'E']
#  0  1  2  3  4
#  A  B  C  D  E
# -5 -4 -3 -2 -1

print(testList[1:4])
# ['B', 'C', 'D']

print(testList[1:-2])
# ['B', 'C']

print(testList[1:])
# ['B', 'C', 'D', 'E']

print(testList[:-3])
# ['A', 'B']

print(testList[:])
# ['A', 'B', 'C', 'D', 'E']

print(testList[::1])
# ['A', 'B', 'C', 'D', 'E']

print(testList[::-1])
# ['E', 'D', 'C', 'B', 'A']

print(testList[::2])
# ['A', 'C', 'E']

Setting or Adding Element to List

testList = [0, 1, 2]


### []
testList[1] = 3
print(testList)
# [0, 3, 2]


### append()
testList.append(5)
print(testList)
# [0, 3, 2, 5]


### insert()
testList.insert(2, 7)
print(testList)
# [0, 3, 7, 2, 5]


### Extend List
testList2 = [10, 11, 12]
testList += testList2
print(testList)
# [0, 3, 7, 2, 5, 10, 11, 12]


### Nested List
testList3 = [20, 21, 22]
nestedList = [testList2, testList3]
print(nestedList)
# [[10, 11, 12], [20, 21, 22]]

Removing Element from List

testList = [0, 1, 2, 3, 4]


### del
del testList[3]
print(testList)
# [0, 1, 2, 4]


### remove()
testList.remove(1)
print(testList)
# [0, 2, 4]

Checking Element in List

testList = [0, 1, 2, 3, 4]

print(2 in testList)
# True

print(7 in testList)
# False

Getting Length of List

testList = [0, 1, 2, 3, 4]

print(len(testList))
# 5

Getting Index of Element in List

testList = ['A', 'B', 'C', 'D', 'E']

print(testList.index('D'))
# 3

Counting Number of Element of List

testList = [0, 1, 1, 2, 3]

print(testList.count(1))
# 2

Getting Min/Max Element in List

testList = [0, 4, 2, 1, 3]

print(min(testList))
# 0

print(max(testList))
# 4

Sorting Elements of List

testList = [0, 4, 2, 1, 3]

sortedList = sorted(testList)
print(sortedList)
# [0, 1, 2, 3, 4]

print(testList)
# [0, 4, 2, 1, 3]

testList.sort()
print(testList)
# [0, 1, 2, 3, 4]

testList.sort(reverse=True)
print(testList)
# [4, 3, 2, 1, 0]

sorted() function creates a new sorted list.

sort() method sorts itself.

Reversing Elements of List

testList = [0, 4, 2, 1, 3]

testList.reverse()
print(testList)
# [3, 1, 2, 4, 0]

Copying List

testList = [0, 1, 2, 3, 4]

copiedList1 = testList          # same as testList
copiedList2 = testList.copy()   # different
copiedList3 = list(testList)    # different
copiedList4 = testList[:]       # different

testList[0] = 5

print(copiedList1)
# [5, 1, 2, 3, 4]

print(copiedList2)
# [0, 1, 2, 3, 4]

print(copiedList3)
# [0, 1, 2, 3, 4]

print(copiedList4)
# [0, 1, 2, 3, 4]

댓글