dev.rean.me
python

05 How to Use List in Python + Tips (Video in Khmer)

Learn Python list with simple example and tips. Add, remove, loop and slice. Short and clear. Good for beginner!

2026-10-19 ยท 4 min read

Share:

Hello friend! ๐Ÿ‘‹ You want keep many value in one variable? Use list!

In this post, I show you how to use list in Python. Easy and short. ๐Ÿ


๐Ÿ“‹ What is List?

List is a group of value in one variable. Use [ ] and put , between value.

fruits = ["apple", "banana", "orange"]

print(fruits)

Output:

['apple', 'banana', 'orange']
  • List can keep text, number, or mix both.
  • List keep the order.
  • List can change (add, remove, edit).
mix = ["Dara", 20, True, 1.75]

๐ŸŽฅ Video: Introduction to List in Python (Khmer)


๐Ÿ”ข Get Item by Index

Index start from 0, not 1.

fruits = ["apple", "banana", "orange"]

print(fruits[0])    # apple   (first)
print(fruits[1])    # banana
print(fruits[-1])   # orange  (last)

โœ‚๏ธ Slice

nums = [10, 20, 30, 40, 50]

print(nums[1:3])    # [20, 30]
print(nums[:2])     # [10, 20]
print(nums[2:])     # [30, 40, 50]

[1:3] mean start at index 1, stop before index 3.


โœ๏ธ Change, Add and Remove

fruits = ["apple", "banana"]

fruits[0] = "mango"          # change
fruits.append("orange")      # add at end
fruits.insert(1, "kiwi")     # add at position 1
print(fruits)                # ['mango', 'kiwi', 'banana', 'orange']

fruits.remove("banana")      # remove by value
last = fruits.pop()          # remove last, and give it back
del fruits[0]                # remove by index
print(fruits)                # ['kiwi']
MethodWhat it do
append(x)Add x at end
insert(i, x)Add x at index i
remove(x)Remove first x
pop()Remove last item
clear()Remove all item

๐Ÿ” Loop Over List

fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

Need index too? Use enumerate():

for i, fruit in enumerate(fruits):
    print(i, fruit)

Output:

0 apple
1 banana
2 orange

๐ŸŽฅ Video: Using List in Python Example 2 (Khmer)


๐Ÿงฐ Useful List Function

nums = [5, 2, 8, 1]

print(len(nums))     # 4      how many item
print(max(nums))     # 8
print(min(nums))     # 1
print(sum(nums))     # 16
print(2 in nums)     # True   check item exist

nums.sort()
print(nums)          # [1, 2, 5, 8]

nums.reverse()
print(nums)          # [8, 5, 2, 1]

๐Ÿงฉ Join Two List

a = [1, 2]
b = [3, 4]

print(a + b)         # [1, 2, 3, 4]

โŒ Common Error

Index too big:

fruits = ["apple", "banana"]
print(fruits[2])

Error:

IndexError: list index out of range

Fix: list with 2 item only have index 0 and 1. Use len(fruits) to check.

Remove item that not exist:

fruits.remove("mango")

Error: ValueError: list.remove(x): x not in list. Check with in first.

if "mango" in fruits:
    fruits.remove("mango")

Copy list wrong way:

a = [1, 2, 3]
b = a          # not real copy!
b.append(4)
print(a)       # [1, 2, 3, 4]  a change too!

Fix: use b = a.copy().


๐Ÿ’ก Tips

  • Use plural name for list: students, fruits, prices.
  • Remember: index start from 0.
  • Use negative index -1 to get last item fast.
  • Use append() to add item. Use for loop to read all item.
  • Use len() to count item before use index.
  • sort() change the original list. Use sorted(nums) if you want keep original.
  • Use copy() when you want new list, not same list.
  • Use list comprehension for short code: squares = [n * n for n in range(5)]

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #List #DataTypes #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: