50% کامل شده تا

List Comprehension

از ویکی‌کد، دانشنامهٔ برنامه‌نویسی
پرش به ناوبری پرش به جستجو
Main Page Tutorial
Python-menu.png
موارد آموزشی
۱خانه
۲مقدمه
۳شروع کار
۴دستور زبان
۵توضیحات
۶متغیرها
۷انواع داده‌ها
۸اعداد
۹تبدیل نوع
۱۰رشته‌ها
۱۱Booleans
۱۲عملگرها
۱۳لیست‌ها
۱۴تاپل‌ها
۱۵مجموعه‌ها
۱۶دیکشنری‌ها
۱۷If...Else
۱۸حلقهٔ While
۱۹حلقهٔ For
۲۰توابع
۲۱Lambda
۲۲آرایه‌ها
۲۳کلاس‌ها / اشیاء
۲۴ارث‌بری
۲۵Iteratorها
۲۶دامنه
۲۷پودمان‌ها
۲۸تاریخ و زمان
۲۹JSON
۳۰RegEx
۳۱PIP
۳۲Try...Except
۳۳ورودی کاربر
۳۴قالب‌بندی رشته
مدیریت فایل
یادگیری ماشین
MySQL در پایتون
MongoDB در پایتون
مرجع پایتون
مرجع ماژول
چگونه در پایتون؟
مثال‌های پایتون


List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.[۱]

Example:

Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.

Without list comprehension you will have to write a for statement with a conditional test inside:

مثال
1 fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
2 newlist = []
3 
4 for x in fruits:
5   if "a" in x:
6     newlist.append(x)
7 
8 print(newlist)


With list comprehension you can do all that with only one line of code:

مثال
1 fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
2 
3 newlist = [x for x in fruits if "a" in x]
4 
5 print(newlist)

The Syntax

newlist = [expression for item in iterable if condition == True]

The return value is a new list, leaving the old list unchanged.


Condition

The condition is like a filter that only accepts the items that valuate to True.

مثال

Only accept items that are not "apple":

newlist = [x for x in fruits if x != "apple"]


The condition if x != "apple" will return True for all elements other than "apple", making the new list contain all fruits except "apple".

The condition is optional and can be omitted:

مثال

With no if statement:

newlist = [x for x in fruits]

Iterable

The iterable can be any iterable object, like a list, tuple, set etc.

مثال

You can use the range() function to create an iterable:

newlist = [x for x in range(10)]


Same example, but with a condition:

مثال

Accept only numbers lower than 5:

newlist = [x for x in range(10) if x < 5]

Expression

The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before it ends up like a list item in the new list:

مثال

Set the values in the new list to upper case:

newlist = [x.upper() for x in fruits]


You can set the outcome to whatever you like:

مثال

Set all values in the new list to 'hello':

newlist = ['hello' for x in fruits]


The expression can also contain conditions, not like a filter, but as a way to manipulate the outcome:

مثال

Return "orange" instead of "banana":

newlist = [x if x != "banana" else "orange" for x in fruits]


The expression in the example above says:

"Return the item if is not banana, if it is banana return orange".


منابع آموزشی