List

  • In Python, a list is a built-in data structure that can hold an ordered collection of items. Unlike arrays in some languages, Python lists are very flexible:
    • Can contain duplicate items
    • Mutable: items can be modified, replaced, or removed
    • Ordered: maintains the order in which items are added
    • Index-based: items are accessed using their position (starting from 0)
    • Can store mixed data types (integers, strings, booleans, even other lists)

시간 복잡도

  • 삽입
    • append(x) -
      • Add an item to the end of the list
    • insert(i, x) -
      • Insert an item at a given position.
  • 삭제
    • remove(x) -
      • Remove the first item from the list whose value is equal to x.
    • pop() -
      • removes and returns the last item in the list.
    • pop(i) -
      • Remove the item at the given position in the list, and return it.
    • del L[i]
  • 접근
    • L[i] -
      • random access

Queue로 사용하고 싶다면

  • It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).
  • To implement a queue, use Deque which was designed to have fast appends and pops from both ends.

Reference