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]
[output]
[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 ที่ยาวกว่า

[2]
[output]
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]

[3]
[output]
[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] คือตัวสุดท้าย

[4]
[output]
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 ใหม่ทั้งก้อน

[5]
[output]
[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 เห็นจะเปลี่ยนตามด้วย

[6]
[output]
[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

[7]
[output]
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

[8]
[output]
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 เดิมไม่เปลี่ยนตาม

[9]
[output]
[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:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. 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 (โครงหัวข้อและแนวคิดหลักของบท)
  2. More on Lists — Python Tutorial — More on Lists (methods such as append) (methods เช่น append)
  3. Sequence Types — list, tuple, range — Built-in Types — Sequence Types (operators, indexing/slicing, in, len) (operators, indexing/slicing, in, len)
  4. Built-in Functions — Built-in Functions (list, max, min, sum)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What is a list in Python?list ใน Python คืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) An ordered sequence of values that can be changedลำดับค่าที่จัดเรียงและเปลี่ยนได้

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) An ordered sequence of values that can be changedลำดับค่าที่จัดเรียงและเปลี่ยนได้
    Correct! A list is an ordered, mutable sequence.ถูกต้อง! list เป็นลำดับที่จัดเรียงและ mutable
  • (B) An ordered sequence that cannot be changedลำดับค่าที่จัดเรียงแต่เปลี่ยนไม่ได้
    Wrong: that is closer to a tuple or a str, not a list.ผิด: นั่นใกล้เคียงกับ tuple หรือ str ไม่ใช่ list
  • (C) Unordered key-value pairsคู่ key–value ที่ไม่เรียงลำดับ
    Wrong: that describes a dict, not a list.ผิด: นั่นคือแนวคิดของ dict ไม่ใช่ list
  • (D) A numbers-only data typeชนิดข้อมูลตัวเลขเท่านั้น
    Wrong: a list can hold many types, not just numbers.ผิด: list เก็บค่าได้หลายชนิด ไม่จำกัดแค่ตัวเลข

ข้อ 2

2 / 10

What is the result of [0, 1] * 3?ผลของ [0, 1] * 3 คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) [0, 1, 0, 1, 0, 1][0, 1, 0, 1, 0, 1]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) [0, 1, 3][0, 1, 3]
    Wrong: * with a list repeats it; it does not append the 3.ผิด: * กับ list คือการทำซ้ำ ไม่ใช่ต่อเลข 3
  • (B) [0, 1, 0, 1, 0, 1][0, 1, 0, 1, 0, 1]
    Correct! [0, 1] repeated 3 times gives six items.ถูกต้อง! [0, 1] ซ้ำ 3 ครั้งได้สมาชิกหกตัว
  • (C) [0, 3, 1, 3][0, 3, 1, 3]
    Wrong: the items are not each multiplied by 3.ผิด: ไม่มีการคูณสมาชิกทีละตัวด้วย 3
  • (D) [3, 3][3, 3]
    Wrong: the items are not replaced by a product.ผิด: ไม่ได้แทนที่สมาชิกด้วยผลคูณ

ข้อ 3

3 / 10

With scores = [85.0, 88.1, 72.9], after scores[1] = 90.0, what is scores[1]?จาก scores = [85.0, 88.1, 72.9] แล้ว scores[1] = 90.0 ค่า scores[1] เป็นอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 90.090.0

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 85.085.0
    Wrong: 85.0 is scores[0], not the position changed.ผิด: 85.0 คือ scores[0] ไม่ใช่ตำแหน่งที่แก้
  • (B) 88.188.1
    Wrong: 88.1 is the old value before it was replaced.ผิด: 88.1 เป็นค่าเดิมก่อนถูกแทนที่
  • (C) 90.090.0
    Correct! Assigning at index 1 gives 90.0.ถูกต้อง! กำหนดค่าด้วยดัชนี 1 แล้วได้ 90.0
  • (D) 72.972.9
    Wrong: 72.9 is scores[2].ผิด: 72.9 คือ scores[2]

ข้อ 4

4 / 10

When y = x with x a list, and you change x[0], what happens?เมื่อ y = x โดย x เป็น list แล้วแก้ x[0] จะเกิดอะไรขึ้น?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) y changes too, because it points to the same listy เปลี่ยนตามด้วย เพราะชี้ list เดียวกัน

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) y does not change, because it is a different listy ไม่เปลี่ยน เพราะเป็นคนละ list
    Wrong: y = x makes them point to the same list.ผิด: y = x ทำให้ชี้ list เดียวกัน
  • (B) It raises an error immediatelyเกิด Error ทันที
    Wrong: assigning by index on a list is allowed.ผิด: การแก้ค่าด้วยดัชนีบน list ทำได้
  • (C) x changes but y is deletedx เปลี่ยนแต่ y ถูกลบ
    Wrong: the variable y is not deleted.ผิด: ไม่มีการลบตัวแปร y
  • (D) y changes too, because it points to the same listy เปลี่ยนตามด้วย เพราะชี้ list เดียวกัน
    Correct! Aliasing means changing through x also affects y.ถูกต้อง! alias ทำให้แก้ผ่าน x กระทบ y ด้วย

ข้อ 5

5 / 10

Which statement about append() versus + is correct?ข้อใดถูกเกี่ยวกับ append() เทียบกับ +?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) append changes the existing list; + builds a new listappend แก้ list เดิม; + สร้าง list ใหม่

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) append changes the existing list; + builds a new listappend แก้ list เดิม; + สร้าง list ใหม่
    Correct! append changes the existing object, while + gives a new list.ถูกต้อง! append แก้วัตถุเดิม ส่วน + ได้ list ใหม่
  • (B) Both always change the existing listทั้งคู่แก้ list เดิมเสมอ
    Wrong: + builds a new list; it does not change the original.ผิด: + สร้าง list ใหม่ ไม่แก้ของเดิม
  • (C) Both always build a new listทั้งคู่สร้าง list ใหม่เสมอ
    Wrong: append does not build a new list; it changes the original.ผิด: append ไม่สร้าง list ใหม่ แต่แก้ของเดิม
  • (D) + changes the existing list; append builds a new list+ แก้ list เดิม; append สร้าง list ใหม่
    Wrong: it is reversed — append changes the original, + builds new.ผิด: สลับกันแล้ว — append แก้เดิม ส่วน + สร้างใหม่

ข้อ 6

6 / 10

What does [] mean in Python?[] ใน Python หมายถึงอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) An empty list with no items yetlist ว่างที่ยังไม่มีสมาชิก

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) An error, because a list must have itemsError เพราะ list ต้องมีสมาชิก
    Wrong: an empty list is allowed.ผิด: list ว่างสร้างได้
  • (B) An empty list with no items yetlist ว่างที่ยังไม่มีสมาชิก
    Correct! [] is an empty list; its len is 0.ถูกต้อง! [] คือ list ว่าง len เป็น 0
  • (C) A list with one item, 0list ที่มีสมาชิกเดียวคือ 0
    Wrong: [0] is different from [].ผิด: [0] ต่างจาก []
  • (D) An empty dictdict ว่าง
    Wrong: an empty dict is {}, not [].ผิด: dict ว่างคือ {} ไม่ใช่ []

ข้อ 7

7 / 10

With l = [1, 9, 6, 5, 0], what is l[1:4]?จาก l = [1, 9, 6, 5, 0] ผลของ l[1:4] คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) [9, 6, 5][9, 6, 5]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) [9, 6, 5][9, 6, 5]
    Correct! Indexes 1, 2, 3 are 9, 6, 5 (4 not included).ถูกต้อง! ดัชนี 1,2,3 คือ 9,6,5 (ไม่รวม 4)
  • (B) [1, 9, 6, 5][1, 9, 6, 5]
    Wrong: it starts at index 1, not 0.ผิด: เริ่มที่ดัชนี 1 ไม่ใช่ 0
  • (C) [9, 6, 5, 0][9, 6, 5, 0]
    Wrong: index 4 (value 0) is not included.ผิด: ดัชนี 4 (ค่า 0) ไม่ถูกรวม
  • (D) [1, 9, 6][1, 9, 6]
    Wrong: that is l[0:3].ผิด: นั่นคือ l[0:3]

ข้อ 8

8 / 10

With l = [1, 9, 6, 5, 0], what is l[-1]?จาก l = [1, 9, 6, 5, 0] ผลของ l[-1] คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 0 (the last item)0 (สมาชิกตัวสุดท้าย)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 1 (the first item)1 (สมาชิกตัวแรก)
    Wrong: a negative index counts from the right; -1 is the last item.ผิด: ดัชนีติดลบนับจากขวา -1 คือตัวสุดท้าย
  • (B) ErrorError
    Wrong: negative indexing is allowed.ผิด: ดัชนีติดลบใช้ได้ปกติ
  • (C) 0 (the last item)0 (สมาชิกตัวสุดท้าย)
    Correct! l[-1] is the last value in the list = 0.ถูกต้อง! l[-1] คือค่าสุดท้ายในลิสต์ = 0
  • (D) 55
    Wrong: 5 is l[-2].ผิด: 5 คือ l[-2]

ข้อ 9

9 / 10

What is the result of sum([2, 0, 2, 4])?ผลของ sum([2, 0, 2, 4]) คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 88

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 4 (the largest value)4 (ค่ามากที่สุด)
    Wrong: that is max, not sum.ผิด: นั่นคือ max ไม่ใช่ sum
  • (B) 20242024
    Wrong: sum adds the values; it does not concatenate digits.ผิด: sum บวกค่า ไม่ได้ต่อเป็นตัวเลขเรียงกัน
  • (C) 4 (the number of items)4 (จำนวนสมาชิก)
    Wrong: that is len, not sum.ผิด: นั่นคือ len ไม่ใช่ sum
  • (D) 88
    Correct! 2 + 0 + 2 + 4 = 8.ถูกต้อง! 2 + 0 + 2 + 4 = 8

ข้อ 10

10 / 10

What is the result of list('App')?ผลของ list('App') คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) ['A', 'p', 'p']['A', 'p', 'p']

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 'App''App'
    Wrong: list() splits a string into single characters.ผิด: list() แยกสตริงเป็นตัวอักษรทีละตัว
  • (B) ['App']['App']
    Wrong: it does not put the whole string as one item.ผิด: ไม่ได้ใส่สตริงทั้งก้อนเป็นสมาชิกเดียว
  • (C) ['A', 'P', 'P']['A', 'P', 'P']
    Wrong: list() does not change the letter case.ผิด: list() ไม่เปลี่ยนตัวพิมพ์
  • (D) ['A', 'p', 'p']['A', 'p', 'p']
    Correct! list() from a string gives a list of its characters.ถูกต้อง! list() จากสตริงได้ list ของตัวอักษร