2

In Python, why do these two 5 * ['th'] and [5 * ' th'] give almost the same result? The question here is why 5* ['th'] gives a list five times big and not five lists.

>>>5 * ['th']
['th', 'th', 'th', 'th', 'th']
>>> [5 * 'th']
['ththththth']

For 5 * ['th'] the expected result is 5 lists!

Varuna
  • 1,343
  • 3
  • 15
  • 30

4 Answers4

7

why do these... give almost the same result?

They don't.

5 * ['th']

This will produce a list with a single element, 'th', and then multiply it by 5. The result is a list with 5 copies of that element:

 ['th', 'th', 'th', 'th', 'th']

The length of this list is 5.


[5 * ' th']

This will concatenate the string ' th' five times, producing the string ' th th th th th', and then create a list with that string as its only element.

[' th th th th th']

The length of this list is 1.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 1
    - "*why do these... give almost the same result?*" -"*They don't.*" It depends on your definition of *almost*. But that's a poorly worded question. I guess, I mean, that the answer should drop unclear and opinionated parts. – luk32 Apr 15 '18 at 19:19
  • @luk32 - The word almost was used because the results of the expressions were not exactly the same.My main focus was why both expressions produced a single list.What I wanted to know was why the expression 5 * ['th'] produced a single list, not five – Varuna Apr 15 '18 at 19:33
  • @Varuna because the *repetition operator*, `*`, works on (almost) any sequence type, returning an object of the same type, but its contents will be *repeated* a number of times. The reason it doesn't return 5 lists is the same reason `5*some_string` will always return a *single* `str` (with length `5*len(some_string)`). – juanpa.arrivillaga Apr 15 '18 at 19:40
  • @Varuna "*What I wanted to know was why the expression 5 * ['th'] produced a single list, not five.*" That would be much better wording IMO, because it can be answered without doing any interpretations and assumptions. The answer delivers nonetheless. – luk32 Apr 15 '18 at 21:26
2

Because 1) the original expressions are almost the same 2) both list and string are iterables and behave very similiarly in Python, so multiplication by an integer is very similiar for list and string, in sense preserving original type of the multiplicand, rather than say yield a tuple or a list of elements of the original type. Interestingly, three of basic four ariphmetic operators return value of the same type as one of operands, though in Python 3 division was redefined to return float even if arguments are integer.

Serge
  • 3,387
  • 3
  • 16
  • 34
2

The list object defines what it thinks multiplication is by how it implements its list.__mul__ and list.__imul__ methods. Python implementers thought that copying the contents N times into a single new list made the most sense for lists.
There is a good reason for that - they wanted multiplication to work like addition:

>>> ['th'] * 5
['th', 'th', 'th', 'th', 'th']
>>> ['th'] + ['th'] + ['th'] + ['th'] + ['th']
['th', 'th', 'th', 'th', 'th']

If you multiply a number by 5 you get a number that's five times as big, not five numbers. Similarly, if you multiply a list by 5, you get a list that's five times big, not five lists.

rob
  • 36,896
  • 2
  • 55
  • 65
tdelaney
  • 73,364
  • 6
  • 83
  • 116
1

The question popped into me when I was going through an example program in Magnus Lie Hetland's book Beginning Python. Part of the answer I received from Magnus when I asked the same question from him is below.

The reason for handling 5 * ['th'] in the way that produces the result ['th', 'th', 'th', 'th', 'th'] is to make multiplication consistent with addition. If you multiply a number by 5 you get a number that's five times as big, not five numbers. Similarly, if you multiply a list by 5, you get a list that's five times big, not five lists.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
Varuna
  • 1,343
  • 3
  • 15
  • 30