List Comprehension In Python

in #programming7 years ago

Hi there. This post is a short overview of list comprehension in the Python programming language. List comprehension is a way of creating lists without the use of for loops. Instead of using for loops and appending items to a list, list comprehension does it all in one.

The full version of this post can be found through the link here. From that link wait for the loading and find the file listComprehension.ipynb in the Jupyter notebook.


Pixabay Image Source

 


A Simple List Of Numbers

 

If you simply want a list of numbers form one to ten, you can use list comprehension as shown below.

one_to_ten = [i for i in range(1, 11, 1)]

print(one_to_ten)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

If you want to use the range() function without the two extra parameters you can do this:

one_to_ten_alt = [i + 1 for i in range(10)]

print(one_to_ten_alt)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 


Pixabay Image Source

 


List Comprehension On A String

List comprehension can be used to extract characters from a string. I can extract each letter from the string Pikachu. Each character in Pikachu is an element in the pikachu_letters list.

pikachu = "Pikachu"

pikachu_letters = [char for char in pikachu]

print(pikachu_letters)

['P', 'i', 'k', 'a', 'c', 'h', 'u']

 

String functions such as .upper() can be used in list comprehension.

pikachu = "Pikachu"

pikachu_cap_letters = [char.upper() for char in pikachu]

print(pikachu_cap_letters)

['P', 'I', 'K', 'A', 'C', 'H', 'U']

 


Pixabay Image Source

 


Including If Statements In List Comprehension

You can add if statements to extract key elements in the list while using list comprehension. The examples below heavily focus on numbers.

In this first example, I extract numbers that are greater than ten from the sample_numbers list.

sample_numbers = [4, -2, 9, 10, 28, 30, 5, 9, 0, 11]

over_10 = [num for num in sample_numbers if num > 10]

print(over_10)
print("Length Of List: {}".format((str(len(over_10)))))

[28, 30, 11]
Length Of List: 3

 

The second example extracts positive whole numbers that are even. It is important that the keyword and is used in the if condition to cover the even number cases and positive number cases.

even_num = [num for num in sample_numbers if (num % 2 == 0) and (num > 0)]

print(even_num)
print("Length Of List: {}".format((str(len(even_num)))))

[4, 10, 28, 30]
Length Of List: 4

 

In the third and last example, random numbers from -1 to +1 are generated Numpy's random.uniform() function. (Note that I did not use a random seed and your randomly generated numbers may differ from mine.)

import numpy as np

random_numbers = np.random.uniform(-1, 1, 50)

print(random_numbers)

[-0.12497176 -0.52754051  0.51200914 -0.18252704 -0.20997032  0.69520343
  0.52815699  0.98940792  0.50802899 -0.47824894 -0.85253566  0.97441918
 -0.22134569 -0.41945919 -0.137236    0.80158944  0.42721549  0.99477487
  0.54247986  0.54221518  0.67179671  0.99909587  0.27257191 -0.18707957
  0.25164118  0.38692985 -0.94933056 -0.32172124 -0.33349099 -0.745678
  0.39537757  0.9811116   0.16365152  0.49243833  0.22757143  0.22031292
  0.48999172 -0.65823286 -0.05085378  0.51539578  0.58079534  0.42183669
 -0.25446919 -0.22452175 -0.64123815  0.75632015  0.96993631 -0.05686616
 -0.25769521  0.34211959]

 

From the randomly generated numbers, I can extract number that are greater than zero. In my instance, 29 out of 50 generated numbers are greater than 0. This is 58% of the 50 numbers.

over_zero = [num for num in random_numbers if num > 0]

print("Length Of List: {}".format((str(len(over_zero)))))
print("Percentage Of Numbers Over Zero: {}%".format((str(100 * len(over_zero)/len(random_numbers)))))

Length Of List: 29
Percentage Of Numbers Over Zero: 58.0%

 


Pixabay Image Source

Coin Marketplace

STEEM 0.05
TRX 0.32
JST 0.080
BTC 66337.87
ETH 1773.94
USDT 1.00
SBD 0.43