Chapter2 - The Basics - Python List
Book - Python Algorithms by Magnus Lie Hetland
Chapter 2 - The Basics
Asymptotic Notation (์ ๊ทผ ํ๊ธฐ๋ฒ)
Itโs about running times.
Example.
append VS. insert
import time
count = 10**5
start = time.time()
nums = []
for i in range(count):
nums.append(i)
nums.reverse()
elapse = time.time()
print("append: ",elapse-start)
start = time.time()
nums = []
for i in range(count):
nums.insert(0,i)
elapse = time.time()
print("insert: ",elapse-start)
##### output #####
# append: 0.024924755096435547
# insert: 4.281127691268921
Adding items to the end of a list scaled better with the list size than inserting them at the front.
Python List
Array VS. List
Commonalities:
- collections of items
- having an order
Differences:
- Array has an index, not List.
- Array occupies consecutive memory spaces.
- This property makes array find element much faster than List.
- Think of Linked List
- Each items are spreaded in memory.
- Each items can be accessed by its address of memory, not index.
Then, why python โListโ has an index?
Actually, itโs a dynamic array, not the list mentioned above. Pythonโs list is implemented like array. As we know, we use pythonโs list as a stack.
In short, pythonโs list is an โarrayโ with high-leveled functioned applied.
Array VS. List: https://velog.io/@choonghee-lee/%EB%B2%88%EC%97%AD-Array-vs.-List-vs.-Python-List
Leave a comment