7.1 Multiplication
Python has a nifty syntax that lets you increase the number of elements using a simple multiplication operator.
String Generation
You can apply it to strings:
print("Mama" * 5) # outputs MamaMamaMamaMamaMama
Or you can quickly output a separator line:
print("-" * 40) # outputs ----------------------------------------
List Generation
You can also multiply the number of elements in a list:
print(["apple"] * 5) # outputs ['apple', 'apple', 'apple', 'apple', 'apple']
This operator has a more practical use too. For example, if you want to create a list of 10 elements filled with zeros:
alist = [0] * 10
print(alist) # outputs [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
You can even make a two-dimensional array. For example, if you want to create a 20x10 array filled with zeros, it's just as easy:
alist = [[0] * 10] * 20
print(alist) # outputs [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ...]
Pretty handy, right?
7.2 List Comprehension
Python has this useful tool called "List Comprehension" — it's a neat and compact way to create lists. This syntactic tool lets you generate new lists by applying an expression to each item in an iterable in a single line of code.
It's easier to show than describe. This syntax looks like this:
[expression for variable in sequence]
Where:
-
variable
— identifier of some variable, -
sequence
— sequence of values that the variable takes (this could be a list, a string, or an object fromrange
), -
expression
— some expression, usually dependent on the variable used in the generator, that fills the list elements.
Important!
The whole expression should be enclosed in square brackets.
Here are a few examples of using generators:
You can create a list with n zeros using a generator:
alist = [0 for i in range(100)]
To create a list filled with squares of integers, you can do this:
n = 5
alist = [i ** 2 for i in range(n)]
Create a list filled with integers from 1 to n:
n = 10
alist = [i + 1 for i in range(n)]
And here's how you can get a list filled with random numbers from 1 to 9 (using randrange()
from the random module):
from random import randrange
n = 10
alist = [randrange(1, 10) for i in range(n)]
7.3 Loop in List Comprehension
List Comprehension isn't just for generating lists. Essentially, it's a compact way of writing a for loop.
For instance, you can easily output some values from a list to the screen:
alist = ["apple", "banana", "cherry"]
[print(x) for x in alist]
Or even input values from the keyboard:
a = [input() for i in range(int(input()))]
In this example, the list consists of strings read from standard input: first, you enter the number of list elements (this value is used as the range
argument), then the specified number of strings.
In the more familiar form, this code looks like this:
n = int(input())
a = []
for i in range(n):
x = input()
a.append(x)
Actually, there are quite a few possibilities with List Comprehension:
You can use conditions:
Including conditions in the expression allows for filtering elements; for example, you can create a list of only even numbers:
[x for x in range(10) if x % 2 == 0]
Nested loops:
The generator allows for nested loops, for example, for creating multidimensional arrays:
[(x, y) for x in range(3) for y in range(3)]
GO TO FULL VERSION