ใน Python จะมีการเก็บข้อมูลที่เป็นชุดอยู่ 4 แบบ คือ

  • list
  • tuple
  • set
  • dictionary

คุณสมบัติของ list ใน Python คือ แสดงข้อมูลตามลำดับ, ข้อมูลใน list สามารถเปลี่ยนแปลงได้ และสามารถมีข้อมูลซ้ำกันได้

สร้าง list

list จะถูกคลุมด้วย square brackets [] และข้อมูลแต่ละตัวจะถูกแยกด้วยเครื่องหมาย comma

ตัวอย่าง

lst = ['Emma', 'John', 'Ted', 'Ann', 'Ted']
print(lst)

output:
['Emma', 'John', 'Ted', 'Ann', 'Ted']

แสดงข้อมูลตามลำดับ และสามารถมีข้อมูลที่ซ้ำกันใน list ได้

reverse ลำดับข้อมูล

เป็นการจัดข้อมูลใน list จากขวาไปซ้าย แทนการจัดจากซ้ายไปขวาแบบปกติ ใช้ reverse() method

ตัวอย่าง

lst = [6, 7, 8, 9]
lst.reverse()
print(lst)

output:
[9, 8, 7, 6]

จัดเรียงข้อมูลตามลำดับ

ใช้ method sort() เพื่อจัดลำดับข้อมูลจากน้อยไปมาก

ตัวอย่าง

lst = [6, 12, 18, 9]
lst.sort()
print(lst)

output:
[6, 9, 12, 18]

เราสามารถจัดเรียงข้อมูลตามลำดับจากมากไปน้อยได้โดยใส่ reverse=True ใน parameter

ตัวอย่าง

lst = [6, 12, 18, 9]
lst.sort(reverse=True)
print(lst)

output:
[18, 12, 9, 6]

กรณีข้อมูลใน list เป็น string method นี้จะเรียงข้อมูลตามลำดับพยัญชนะจาก A-Z และจากตัวพิมพ์ใหญ่ไปตัวพิมพ์เล็ก

ตัวอย่าง

lst = ['c', 'a', 'b', 'A', 'C', 'B']
 lst.sort()
 print(lst)

output:
['A', 'B', 'C', 'a', 'b', 'c']

หากใส่ reverse=True ใน parameter การจัดเรียงลำดับจะเรียงจาก Z-A และจากตัวพิมพ์เล็กไปหาตัวพิมพ์ใหญ่แทน

ตัวอย่าง

lst = ['c', 'a', 'b', 'A', 'C', 'B']
 lst.sort(reverse=True)
 print(lst)

output:
['c', 'b', 'a', 'C', 'B', 'A']

หา index ของข้อมูล

ใช้ index() method เพื่อหาตำแหน่ง index ของข้อมูลที่ต้องการ กรณีที่มีข้อมูลมากกว่า 1 ตัวใน list ตำแหน่งที่ได้จะเป็นตำแหน่งของข้อมูลตัวแรกที่พบใน list ก่อนเท่านั้น (นับจากซ้ายไปขวา)

ตัวอย่าง

lst = [6, 7, 8, 9]
x = lst.index(8)
print(x)

output:
2

เข้าถึงข้อมูล

การเข้าถึงข้อมูลใน list หรือเรียกว่า items สามารถทำได้โดยการอ้างถึงตำแหน่ง index (ตำแหน่ง index จะเริ่มนับที่ 0 เป็นต้นไป ดังนั้นตำแหน่งแรกใน list มีตำแหน่ง index เป็น 0)

ตัวอย่าง

lst = ['Emma', 'John', 'Ted', 'Ann', 'Ted']
print(lst[0])

output:
Emma

Negative Indexing

การ index แบบให้ค่าติดลบ คือการอ้างตำแหน่งจากหลังมาหน้า ใช้ตัวเลขจำนวนเต็มติดลบ ค่า index แรกที่นับคือ -1 ตัว item ที่มีค่า index เท่ากับ -1 คือตัวสุดท้ายใน list

ตัวอย่าง

lst = ['Emma', 'John', 'Ted', 'Ann', 'Ted']
print(lst[-4])

output:
John

Range indexes

สามารถกำหนดค่า range เพื่อแสดงจำนวนสมาชิกตั้งแต่ตำแหน่งหนึ่งไปจนถึงตำแหน่งหนึ่งใน range ได้ (ตัวสุดท้ายเป็น exclusive)

list[start:end]

ค่าที่ได้จะเป็น list ตัวใหม่ เป็นการสร้าง list ใหม่แบบ deep copy

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
favourite = bakery[1:4]
bakery[2] = 'doughnut'
print(bakery)
print(favourite)

output:
['candy', 'pie', 'doughnut', 'pastry', 'cookies']
['pie', 'cake', 'pastry']

ในตัวอย่างมี list ชื่อ bakery มี items ทั้งหมด 5 ตัว ต่อมาให้ตัวแปรชื่อ favourite มี items เป็น items ของ bakery ใน range 1:4 (ตัวที่ 4 เป็น exclusive หมายความว่าไม่นับตัวที่ 4 ให้หยุดแค่ตัวที่ 3 พอ)
จากนั้นเปลี่ยนข้อมูลที่อยู่ในตำแหน่งที่ 2 ของ list bakery
เมื่อเรียกแสดง items ของ list ทั้งสองอันจะเห็นว่าการเปลี่ยนข้อมูลของ list แรกไม่มีผลต่อ list อันใหม่

หากเว้นค่า start การแสดงผลของ list จะแสดงผลตั้งแต่ item แรกของ list ไปจนถึง item ที่อยู่ในตำแหน่ง end แบบ exclusive

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
favourite = bakery[:4]
print(favourite)

output:
['candy', 'pie', 'cake', 'pastry']

หากเว้นค่า end การแสดงผลของ list จะแสดงผลตั้งแต่ item ของตำแหน่งที่ระบุในค่า start ไปจนถึง item สุดท้ายใน list

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
favourite = bakery[1:]
print(favourite)

output:
['pie', 'cake', 'pastry', 'cookies']

Range ติดลบ

คือการ range จากหลังมาหน้า ตัวที่ติดลบจะนับจากด้านหลัง ตัวที่ไม่ได้ติดลบจะนับจากด้านหน้าปกติ ผลลัพธ์คือ items ที่อยู่ในระหว่าง range นี้

start เป็นจำนวนเต็มลบ end เป็นจำนวนเต็มบวก

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
favourite = bakery[-2:5]
print(favourite)

output:
['pastry', 'cookies']

start เป็นจำนวนเต็มบวก end เป็นจำนวนเต็มลบ

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
favourite = bakery[1:-1]
print(favourite)

output:
['pie', 'cake', 'pastry']

start และ end เป็นจำนวนเต็มลบ

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
favourite = bakery[-4:-2]
print(favourite)

output:
['pie', 'cake']

เวลาเก็บ items ที่อยู่ใน range มาใส่ใน list ใหม่ จะนับจากค่า start ไปหาค่า end ทางขวามือ ดังนั้นไม่ว่าค่าทั้งสองจะเป็นจำนวนเต็มลบหรือเต็มบวกก็ตาม หากต้องนับค่า start ไปหาค่า end ทางซ้ายมือ จะได้ list ว่าง []

เปลี่ยนค่าของ item

การเปลี่ยนค่าของ item ทำได้โดยการอ้างถึงค่า index ของ item นั้น

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
bakery[2] = 'doughnut'
print(bakery)

output:
['candy', 'pie', 'doughnut', 'pastry', 'cookies']

Loop List

ลูป items ทั้งหมดใน list ออกมา โดยใช้ for loop จะได้ item ทีละตัวในแต่ละแถว

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
 for x in bakery:
   print(x)

output:
candy
pie
cake
pastry
cookies

Check item ใน list

ใช้ if…in keyword เพื่อดูว่ามี item นั้นๆใน list หรือเปล่า

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
 if 'croissant' in bakery:
   print('croissant is in the list')
 else:
   print('croissant is not in the list')

output:
croissant is not in the list

นับ items ใน list

ใช้ฟังก์ชั่น len() เพื่อหาจำนวน items ใน list

ตัวอย่าง

bakery = ['candy', 'pie', 'cake', 'pastry', 'cookies']
print(len(bakery))

output:
5

นับว่ามี item นั้นกี่อัน

count() method ใช้นับว่ามี item นั้นกี่อันใน list

ตัวอย่าง

students = ['Emma', 'John', 'Ted', 'Ann', 'Ted']
tedCount = students.count('Ted')
print(tedCount)

output:
2

จากตัวอย่างต้องการหาว่ามีคนชื่อ Ted กี่คน ได้คำตอบคือ 2 คน หมายความว่า ใน list ชื่อ students มี item ชื่อ Ted ซ้ำกัน 2 อัน

หากไม่มี item ใดที่ซ้ำกัน จะได้ค่า 1 เพราะมีแค่ 1 จำนวนใน list

เพิ่ม item

append() method

ใช้ในการเพิ่ม item ให้แก่ list

ตัวอย่าง

bakery = ['cake', 'pastry', 'pie']
bakery.append('bread')
print(bakery)

output:
['cake', 'pastry', 'pie', 'bread']

insert() method

ใช้เพิ่ม item แบบเฉพาะที่ โดย parameter แรก ให้ใส่ค่า index ที่ต้องการเพิ่ม item (เริ่มที่ 0) ใน parameter ต่อมาให้ใส่ item ที่ต้องการเพิ่ม

ตัวอย่าง

bakery = ['cake', 'pastry', 'pie']
bakery.insert(0, 'bread')
print(bakery)

output:
['bread', 'cake', 'pastry', 'pie']

ลบ item ออกจาก list

remove() method

ใช้ลบ item ใด item หนึ่งที่ต้องการเท่านั้น

ตัวอย่าง

bakery = ['cake', 'pastry', 'pie']
bakery.remove('cake')
print(bakery)

output:
['pastry', 'pie']

pop() method

ใช้ลบ item ใด item หนึ่งโดยระบุ index ของ item ที่ต้องการลบ หากไม่ระบุ index จะลบ item สุดท้ายใน list

ตัวอย่าง

bakery = ['cake', 'pastry', 'pie', 'bread']
bakery.pop()
print(bakery)
bakery.pop(0)
print(bakery)

output:
['cake', 'pastry', 'pie']
['pastry', 'pie']

del keyword

ใช้ลบ item ด้วยการระบุ index ของ item นั้น

ตัวอย่าง

bakery = ['cake', 'pastry', 'pie', 'bread']
del bakery[1]
print(bakery)

output:
['cake', 'pie', 'bread']

clear() method

ใช้ลบ items ทั้งหมดออกจาก list

ตัวอย่าง

bakery = ['cake', 'pastry', 'pie']
bakery.clear()
print(bakery)

output:
 []

คัดลอก List

เราไม่สามารถคัดลอก list โดยการ assign list หนึ่ง ไปยังอีก list หนึ่งได้ เพราะจะทำให้เกิด shallow copy

shallow copy จะแชร์ข้อมูลกันระหว่าง list ทั้งสองอัน หากเปลี่ยนข้อมูลใน list ใด list หนึ่ง ข้อมูลใน list อื่นที่แชร์ข้อมูลเดียวกันจะเปลี่ยนไปด้วย

ตัวอย่าง

lst1 = [6,7,8,9]
lst2 = lst1
lst1[1] = 33
lst2[2] = 66
print(lst1)
print(lst2)

output:
[6, 33, 66, 9]
[6, 33, 66, 9]

copy() method

เป็น built-in method ที่ช่วยคัดลอก list และได้เป็น deep copy แทน shallow copy

lst1 = [6,7,8,9]
lst2 = lst1.copy()
lst1[1] = 33
lst2[2] = 66
print(lst1)
print(lst2)

output:
[6, 33, 8, 9]
[6, 7, 66, 9]

list() method

เป็น built-in method ที่ใช้ในการคัดลอก list แบบ deep copy เช่นกัน

ตัวอย่าง

lst = [6, 7, 8, 9]
newlst = list(lst)
newlst[1] = 11
lst[1] = 22
print(lst)
print(newlst)

output:
[6, 22, 8, 9]
[6, 11, 8, 9]

รวม list เข้าด้วยกัน

Operator +

มีวิธีรวม list จากหลาย list ให้เป็น list เดียวหลายวิธี เรียกว่า การ join list วิธีที่ง่ายที่สุด คือการใช้ operator บวก (+)

ตัวอย่าง

lst1 = [1, 2, 3, 4]
lst2 = ['a', 'b', 'c', 'd']
lst3 = [1.2, 2.3, 3.4, 4.5]
lst4 = lst1 + lst2 + lst3
print(lst4)

output:
[1, 2, 3, 4, 'a', 'b', 'c', 'd', 1.2, 2.3, 3.4, 4.5]

append() method

อีกวิธีหนึ่งที่สามารถใช้ join list ได้ คือการใช้ append() method ร่วมกับ for…loop โดยลูป item ของ list หนึ่งไปยังอีก list หนึ่งทีละตัว

ตัวอย่าง

lst = ['a', 'b', 'c', 'd']
lst2 = [1, 2, 3]

for x in lst2:
  lst.append(x)

print(lst)

output:
['a', 'b', 'c', 'd', 1, 2, 3]

extend() method

ใช้ join list โดยไม่ต้องใช้ for…loop

ตัวอย่าง

lst = ['a', 'b', 'c', 'd']
lst2 = [1, 2, 3]

lst.extend(lst2)
print(lst)

output:
['a', 'b', 'c', 'd', 1, 2, 3]

list() Constructor

สามารถสร้าง list ตัวใหม่ได้จาก constructor ใน statement จะต้องสร้าง tuple ก่อน จากนั้นแปลงเป็น list โดยใช้ list() ฟังก์ชั่น

lst = list(('a', 'b', 'c'))
print(lst)

output:
['a', 'b', 'c']

List Built-in Methods

MethodsDescription
append()เพิ่ม item สุดท้ายใน list
clear()ลบ items ทั้งหมดจาก list
copy()สร้างและ return deep copy ของ list
count()นับจำนวนของ item ใด item หนึ่งใน list
extend()เพิ่ม item ในตำแหน่งสุดท้ายของ list (item ใดก็ได้ที่เป็น iterable)
index()คืนค่า index ของ item แรกที่ต้องการค้นหา
insert()เพิ่ม item ในตำแหน่ง index ที่ต้องการ
pop()ลบ item ออกจากตำแหน่ง index ที่กำหนดหรือตำแหน่งสุดท้าย
remove()ลบ item ที่กำหนดออกจาก list
reverse()จัดเรียงลำดับข้อมูลใน list แบบหลังไปหน้า
sort()จัดเรียงลำดับข้อมูลใน list ตามลำดับพยัญชนะ
Advertisement