Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Định nghĩa list trong Python

List là sequence data type. Comma-separated elements (không cần cùng type) trong ngoặc vuông [ ]. List là ordered collection. Truy cập element bằng zero-based index.

Ví dụ

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]

Empty list

list1 = []

Operators cơ bản

  • len(list): độ dài
  • list[index]: truy cập element
  • list.append(x): thêm cuối
  • list.insert(i, x): chèn tại vị trí
  • list.remove(x): xóa first occurrence
  • list.pop(i): xóa và trả về element tại index