Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การทำงานของ * Operator บน List ใน Python

Star * operator unpacks sequence/collection เป็น positional arguments หากมี list ต้องการส่ง items เป็น arguments ตามตำแหน่ง แทน index ทีละตัว ใช้ * operator

ตัวอย่าง

def multiply(a, b):
    return a * b

values = [1, 2]
print(multiply(*values))

Unpack list ให้ execute จริงเป็น:

print(multiply(1, 2))

ผลลัพธ์

2