Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Sequence Operations 0:57
- Slices 4:29
- Creating Slices 1 objective
- Len, Min, and Max 3:04
- Finding len, min, max 3 objectives
- Membership Testing 2:16
- Count and Index 4:38
- Code Samples: Membership Testing, Count, and Index
- Concatenation and Multiplication 3:04
- Sequence Operations Cheat Sheet
- Recap of Sequence Operations 4 questions

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Learn how to combine sequences with concatenation and multiplication
The python docs describe the effects of concatenating and multiplying immutable objects:
Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below:
if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete
if concatenating bytes objects, you can similarly use bytes.join() or io.BytesIO, or you can do in-place concatenation with a bytearray object. bytearray objects are mutable and have an efficient overallocation mechanism
if concatenating tuple objects, extend a list instead
This might all sound rather complicated, but the gist is that sometimes new Python sequences are built up by looping over an existing sequence and concatenating each item onto the new sequence. Using a tuple or string for this would be very costly in terms of memory and time!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Leah Kelly
4,141 Pointsstr looks like a function, so why is it acting like a variable?
Posted by Leah KellyLeah Kelly
4,141 Points3 Answers
-
heebers
Python Development Techdegree Student 1,835 Points1 Answer
-
rosieg
1,666 Points2 Answers
-
Michael Cronk
8,970 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up