search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Python | List Comprehension

schedule Aug 12, 2023
Last updated
local_offer
Python
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

List comprehension in Python allows us to create a new list based on the values of an existing list in a short, efficient manner.

Syntax

Basic

[output expression for variable in iterable]

Conditional

[output expression + conditional on output for variable in iterable + conditional on iterable]

Examples

Basic

If we have an existing list of numbers called nums:

nums = [5, 9, 30, 22, 13]

And we want to create a new list with values one greater than the current values in nums.

Using list comprehension this can be achieved using the below:

new_nums = [num + 1 for num in nums]
print(new_nums)
[6, 10, 31, 23, 14]

Without using list comprehension this would look a lot longer:

new_nums = []
for num in nums:
new_nums.append(num + 1)

Nested

We can also leverage list comprehension to produce nested loops:

num_pairs = [(num1, num2) for num1 in [0,1] for num2 in [4,5]]
print(num_pairs)
[(0, 4), (0, 5), (1, 4), (1, 5)]

Conditional

To specify condition on output:

[num if num % 3 == 0 else 0 for num in range(10)]
[0, 0, 0, 3, 0, 0, 6, 0, 0, 9]

Here for each number from 0 - 9 (specified by range(10)), we add the number to the list if it is perfectly divisible by 3, or we add 0 to the list if the number is not perfectly divisible by 3.

To specify condition on input:

[num for num in range(10) if num%3 == 0]
[0, 3, 6, 9]

Here for each number from 0-9 (specified by range(10)), we first check whether the number is perfectly divisible by 3. If it is we add that number to the returned list, if not we do not add anything and simply move to check the next number in the iterable.

To specify condition on both input and output:

[num + 10 if num % 2 ==0 else 0 for num in range(10) if num%3 == 0]
[10, 0, 16, 0]

Finally, here we first specify a condition on the input if num % 3 ==0. If the number is perfectly divisible by 3, we then check whether the number is divisible by 2, and if so we add the number + 10 to the list, or 0 otherwise.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!