Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialneal nakagawa
3,409 PointsWhy does rainbow[4:5] add to the list and not replace the items in the list at the #4 index? Kenneth's earlier examples
Why does rainbow[4:5] add to the list and not replace the items in the list at the #4 index? Kenneth's earlier examples of [2:4] showed that he replaced ["green","yellow"] with ["yellow","green"].
Gemunu Amarasinghe
6,991 Pointsrainbow[4:5] is one item (include 4, exclude 5). Replace [blue] with [blue, indigo]
rainbow[2:4] is two items (include 2 & 3, exclude 4). Replace [green, yellow] with [yellow, green]
1 Answer
Iulia Maria Lungu
17,546 PointsI suggest to rewatch the video and pause it. You'll see that doing a rainbow[4:5] = ["blue", "indigo"] does not actually add to the the list, it is a replace that happens as well as when replacing the ["green","yellow"] by ["yellow","green"]. What rainbow[4:5] = ["blue", "indigo"] does is:
- drop "blue" cause "blue" is at index 4
- add both "blue" and "indigo"
You thought "indigo" was added but instead truth is "blue" is replaced by "blue", "indigo". Of course what was next to "blue" before now is shifted to the right by one position.
Chandelor Simon
2,242 PointsChandelor Simon
2,242 PointsIulia Maria Lungu is right. I see why you were tripped up though because what's being replaced is included in what it's being replaced with. If you're still iffy on this, think of it this way:
with index 4 of our list(rainbow) still being "blue": if we do rainbow[4:5] = ["cats", "dogs"], then (at this point in the video) when we enter in rainbow, we'd get:
['red', 'orange', 'yellow', 'green', 'cats', 'dogs', 'purple', 'pink']
If index 4 (blue) is x, then we are replacing it with y & z - it just so happens that x = y (blue) in this case scenario.