216k views
2 votes
If a is [1, 2, 3], what is the difference (if any) between a*3 and [a, a, a]?

User Rocketq
by
6.5k points

1 Answer

3 votes

Answer:

a*3 = [1, 2, 3, 1, 2, 3, 1, 2, 3]

[a, a, a] = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

Step-by-step explanation:

Given the following :

a = [1, 2, 3]

a defined above is a list which contains the the digits 1, 2 and 3. Lists are mutable, that is its inputs can be changed.

a*3 creates a Repetition of the items in list a, 3 times, hece, the output will be :

[1, 2, 3, 1, 2, 3, 1, 2, 3]

[a, a, a] will create a list of lists, with each list item of a embedded within another list, hence, the output will be :

[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

Hence,

a*3 produces a repeated list of [1, 2, 3, 1, 2, 3, 1, 2, 3]

[a, a, a] produces a list of lists : [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

User Pgb
by
6.8k points