So the question is from
Using list comprehension create the following list of tuples:
[(0, 1, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1, 1),
(2, 1, 2, 4, 8, 16, 32),
(3, 1, 3, 9, 27, 81, 243),
(4, 1, 4, 16, 64, 256, 1024),
(5, 1, 5, 25, 125, 625, 3125),
(6, 1, 6, 36, 216, 1296, 7776),
(7, 1, 7, 49, 343, 2401, 16807),
(8, 1, 8, 64, 512, 4096, 32768),
(9, 1, 9, 81, 729, 6561, 59049),
(10, 1, 10, 100, 1000, 10000, 100000)]
The following is my solution:
powers_list = [tuple([i] + [i**j for j in range(6)]) for i in range(11)]
print(powers_list)
This next part I got form GeeksForGeeks:
matrix = []
for i in range(11):
# Append an empty sublist inside the list
matrix.append([i])
for j in range(6):
matrix[i].append(i**j)
print(matrix)
I understand the geeks for geeks part. However, the list comprehension is confusing to me. Why and How does
[i] + [i**j..]]
[i]
append/prepend to the list instead of resulting in [[0], [1, 0, 0, 0, 0]]
?
Does the +
function as listA.extend(listB)
?
List comes under sequence class and sequence provide a method of concatenation similar to your concatenation in your string .
by concatenation
a=[1,2,3]
b=[2,3,4]
c=a+b
c=[1,2,3,2,3,4]