# Python: Multiplying sequences

Written by amagard on Tue Oct 30, 2012 1:58 pm in blog Blog around the clock under python -
626 views

Sequences in Python can be multiplied, but while 36*10 yields 360 [36]*10 yields [36, 36, 36, 36, 36, 36, 36, 36, 36, 36].

Again, this can be used for strings as well:

Code: |

>>> "-"*78 '------------------------------------------------------------------------------' |

And how about tuples ? Does it work for tuples as well ?

Code: |

>>> (36)*10 360 |

Doesn't look like. Well, in this case the brackets are interpreted as brackets like typically used in formulas to define priorities in terms of what to compute first.

How can we change that and convince Python to multiply a tuple ?

Code: |

>>> (36,)*10 (36, 36, 36, 36, 36, 36, 36, 36, 36, 36) |

Or:

Code: |

>>> tuple([36])*10 (36, 36, 36, 36, 36, 36, 36, 36, 36, 36) |

**0 blog comments below**