ได้ลิสต์สตริง อาจมีสตริงว่างหน้าหลายตัว ต้องการสตริงแรกที่ไม่ใช่สตริงว่าง
วิธี 1: ใช้ next() กับ Generator
listA = ['', 'top', 'pot', 'hot', ' ', 'shot']
res = next(s for s in listA if s)
print(res) # top
วิธี 2: ใช้ filter() (Python 2) / list(filter(...))[0] (Python 3)
listA = ['', 'top', 'pot', 'hot', ' ', 'shot']
# Python 3:
res = next(filter(None, listA))
print(res) # top
หมายเหตุ
if s และ filter(None, ...) จะถือว่า '', 0, False, None, [] เป็น False ส่วน ' ' (space) เป็น True