List: an ordered sequence you can change List: ลำดับค่าที่เปลี่ยนได้
Build a list; compare / join / repeat; indexing–slicing and changing items; membership; built-in functions and append() สร้าง list, เปรียบเทียบ / ต่อ / ซ้ำ, indexing–slicing และการแก้ไขค่า, membership, ฟังก์ชัน built-in และ append()
A list is an ordered sequence of values that can be changed (mutable). Use it to
store several values without knowing how many in advance. This lesson builds a list with [...],
compares, joins and repeats lists, uses index/slice, changes an item in place, checks membership with
in, uses built-in functions, and the append() method.
list คือลำดับค่าที่จัดเรียงและเปลี่ยนได้ (mutable)
ใช้เก็บหลายค่าโดยไม่ต้องรู้จำนวนล่วงหน้า
บทนี้จะสร้าง list ด้วย [...] เปรียบเทียบ ต่อและทำซ้ำ ใช้ดัชนี/slice แก้ค่าในตำแหน่ง
ตรวจสมาชิกด้วย in ใช้ฟังก์ชันในตัว และเมธอด append()
1 List literals 1 List literals
Write a list with square brackets [...], separating items with commas. [] is an empty
list — no items yet. Values of different types can be mixed, though it is generally not recommended.
เขียน list ด้วยวงเล็บเหลี่ยม [...] คั่นสมาชิกด้วยจุลภาค
[] คือ list ว่าง — ค่าว่างที่ยังไม่มีสมาชิก
ค่าต่างชนิดใส่รวมกันได้ แต่โดยทั่วไปไม่แนะนำ
[1, 2, 3] []
2 Comparison 2 Comparison
Compare lists with < <= > >= by comparing items
pair by pair from left to right — the first differing pair decides the result. If all compared items are
equal until one side runs out, the shorter list is the smaller one.
เปรียบเทียบ list ด้วย < <= > >=
โดยเทียบสมาชิกทีละคู่จากซ้ายไปขวา — คู่แรกที่ต่างกันตัดสินผล
ถ้าเท่ากันหมดจนหมดฝั่งใดฝั่งหนึ่ง ให้เทียบความยาว (สั้นกว่าถือว่าน้อยกว่า)
2.1 Try comparing lists 2.1 ทดลองเปรียบเทียบ list
For example, [2, 0, 2, 4] < [2, 0, 3, 0] is True because at the third position
2 < 3. A shorter list with the same start is smaller than a longer one.
เช่น [2, 0, 2, 4] < [2, 0, 3, 0] เป็น True เพราะที่ตำแหน่งที่สาม 2 < 3
ส่วน list สั้นกว่าที่ขึ้นต้นเหมือนกันจะน้อยกว่า list ที่ยาวกว่า
True False False True
3 Concatenation / repetition 3 Concatenation / Repetition
With lists: + joins them into a new list (concatenation) and * repeats the items a
whole number of times (repetition).
กับ list: + ต่อเป็น list ใหม่ (concatenation) และ * ทำซ้ำสมาชิกตามจำนวนเต็ม (repetition)
3.1 Try + and * 3.1 ทดลอง + และ *
[1965] + [2021] gives [1965, 2021]; [0, 1] * 3 gives
[0, 1, 0, 1, 0, 1].
[1965] + [2021] ได้ [1965, 2021]
ส่วน [0, 1] * 3 ได้ [0, 1, 0, 1, 0, 1]
[1965, 2021] [0, 1, 0, 1, 0, 1]
4 Indexing / slicing 4 Indexing / Slicing
Indexing starts at 0 — l[i] gets an item, l[a:b] takes a range (not
including b), and l[a:b:c] adds a step. A negative index counts from the
right, e.g. l[-1] is the last item.
ดัชนีเริ่มที่ 0 — ใช้ l[i] ดึงค่า, l[a:b] ตัดช่วง (ไม่รวม b),
และ l[a:b:c] เพิ่ม step ดัชนีติดลบนับจากขวา เช่น l[-1] คือตัวสุดท้าย
1 [9, 6, 5] [9, 5] 0
5 Change an item by index 5 แก้ค่าด้วย index
Because a list is mutable, you can assign by index, e.g. scores[2] = 50.0
replaces the item at position 2 without rebuilding the whole list.
เพราะ list เป็น mutable จึงกำหนดค่าด้วยดัชนีได้ เช่น scores[2] = 50.0
จะแทนที่สมาชิกตำแหน่งที่ 2 โดยไม่ต้องสร้าง list ใหม่ทั้งก้อน
[85.0, 88.1, 50.0, 63.4]
6 Alias / mutable 6 Alias / mutable
When y = x and x is a list, both names point to the same list (an
alias). Changing it through x[0] = ... also changes what y sees.
เมื่อ y = x และ x เป็น list ทั้งสองชื่อชี้ list เดียวกัน (alias)
ถ้าแก้ผ่าน x[0] = ... ค่าที่ y เห็นจะเปลี่ยนตามด้วย
[2024, 2022] [2024, 2022]
7 Membership
in / not in check whether a value is in the list — they return True
or False.
in / not in ตรวจว่ามีค่าอยู่ใน list หรือไม่ — คืน True หรือ False
False True
8 List functions 8 List functions
Built-in functions often used with lists: len, list, max, min,
sum.
ฟังก์ชันในตัวที่ใช้กับ list บ่อย: len, list, max, min, sum
8.1 len / list / max / min / sum
len(l) counts the items, list(...) builds a list from a string or a range,
max / min find the largest/smallest, sum adds up the numbers in a list.
len(l) นับจำนวนสมาชิก, list(...) สร้าง list จากสตริงหรือ range,
max / min หาค่ามาก/น้อยสุด, sum รวมค่าตัวเลขใน list
4 ['A', 'p', 'p'] [0, 1, 2, 3, 4] 4 0 8
9 Did you know: append 9 Did you know: append
The .append(x) method adds x to the end of the existing list (it
changes the object in place and returns None). This is different from +, which builds
a new list that you usually rebind to a name.
เมธอด .append(x) เพิ่ม x ท้าย list เดิม (แก้ที่วัตถุเดิม คืน None)
ต่างจาก + ที่สร้าง list ใหม่แล้วมักต้องผูกชื่อใหม่
9.1 append versus + 9.1 append เทียบกับ +
years.append(2030) affects other variables pointing to the same list. years = years +
[2030] builds a new list — the old alias does not change.
years.append(2030) กระทบตัวแปรอื่นที่ชี้ list เดิม
ส่วน years = years + [2030] สร้าง list ใหม่ — alias เดิมไม่เปลี่ยนตาม
[2021, 2022, 2030] [2021, 2022, 2030] [2021, 2022, 2030] [2021, 2022]
Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab
References References / แหล่งอ้างอิง
Sources used for this lesson:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
- G3 Computing Textbook (O-Level/G3, Fourth Version, 2024) — Curriculum Planning and Development Division, Ministry of Education, Singapore — Section 4.12 Lists (chapter outline and key concepts) Section 4.12 Lists (โครงหัวข้อและแนวคิดหลักของบท)
-
More on Lists —
Python Tutorial — More on Lists
(methods such as
append) (methods เช่นappend) -
Sequence Types — list, tuple, range —
Built-in Types — Sequence Types
(operators, indexing/slicing,
in,len) (operators, indexing/slicing,in,len) -
Built-in Functions —
Built-in Functions
(
list,max,min,sum)