-1

Here is the problem statement:

return list[index:]+list[:index+3]

I know [:] represents all the elements of the list. what does this "+3" represents here?

  • 3
    `index` is a particular position in your list. `index+3` is a position 3 places later. – khelwood Nov 09 '18 at 14:02
  • See [Understanding Python's slice notation](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) – khelwood Nov 09 '18 at 14:03
  • 1
    To dive more into the details: It means `index.__add__(3)`. The result is trivial if `index` is an integer and can be pretty much everything if `index` is an other type. – Klaus D. Nov 09 '18 at 14:06
  • Possible duplicate of [Understanding Python's slice notation](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) – qdread Nov 09 '18 at 14:16
  • 1
    The meaning of `some_int + 3` inside the square brackets is exactly the same as outside the square brackets. – Kevin J. Chase Nov 09 '18 at 14:45

2 Answers2

2

for this line:

list[:index+3]

if index is set to 1, it is the same as

list[:4]

it's a simple sum on a variable, meaning it will read to 3 positions after your index variable

Rodolfo Donã Hosp
  • 1,037
  • 1
  • 11
  • 23
  • Please give the credit where credit is due and accept this as your answer if so – Jab Nov 09 '18 at 14:10
1

Every element from [index] till the end plus every element from beginning up to (but not including) [index+3]
Let's have a look at an example:

>>> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
>>> index=2
>>> list[index:]+list[:index+3]
[3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5]

Here index is 2, thus list[index:]+list[:index+3] is exactly the same as list[index:]+list[0:2+3] which is list[index:]+list[0:5]. So, Every element from [2] till the end plus every element from beginning up to (but not including) [5]

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93