Python Range and Extended Lists

·

4 min read

If Python is not your first programming language, and if we you have come from background of C and C++, then sometimes you are writing the program in you old programming language way not the Python way. This can cause efficiency problems and long and messy codes. Today, we are going to learn how to use Python ranges and Python Extended Slice operations to mke your program lot smaller.

Suppose, you need a program to find array of first 10 even numbers. This looks easy and let's see how can we do this in C. Here, I will be focusing only on logic and neglecting some syntax rules of C like declaration of standard input, output etc.

void main() {
    int nums[10] = 0;

    int pointer = 0;
    for (int i=0; i<10; i+=2) {
        nums[pointer] = i;
        pointer++;
    }

    print_array(nums); // an abstract function that prints array
}

But, with use of Python range you can just do;

print(range(0, 10, 2))

and that's it. So, let's dive how this works.

Python Ranges

Python ranges returns a sequence of numbers. The syntax is range(x, y, z) where,

  • x means the start value,

  • y means the stop value, and,

  • z means the step size.

The above line returns a sequence of number starting from x, and ending with y but not including y each time incremented by z units. Also, the value of z if not specified is 1.

Python Ranges Examples

Let's look at few examples;

range(1, 10)
# Outputs [1, 2, 3, 4, 5, 6, 7, 8, 9]

This means sequence of number starting with 1 and ending at 10 but not including 10. Since, step size is not mentioned it is 1.

Similarly, let's look at another example;

range(1, 10, 3)
# outputs [1, 4, 7]

This means sequence of number starting with 1 and ending at 10 not including 10 with step size 3. So, first number is 1, second is 1+3 = 4, then 4+3=7, then 7+3=10, but the sequence should not include 10 so it stops here.

Extended Slicing

Another important thing for you to learn can be extended lists in python. Extended lists is use to perform advanced slicing of the Python lists. So, lets look its syntax first.

The syntax for extended slice is a[x:y:z] where,

  • x is starting index (inclusive),

  • y is stopping index (exclusive),

  • z is step size.

Yeah, the syntax is similar to the range that's why we have covered both topic in same blog. Also, remember the default value for x is 0 (start of the list), y is last index of the list, and z is 1.

To clear this, lets look at a few examples;

Examples of Python Extended Lists

For all examples, we will be using variable a to see the examples here, the value of a is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

a = range(0, 10)

If we want number between 2nd and 6th index then we can do,

a[2:6]
# outputs [2, 3, 4, 5]

What if we omitted the end index value,

a[2:]
# outputs [2, 3, 4, 5, 6, 7, 8, 9]

This works because the default value for stop value in slicing is the end of the list so, it means start from 2th position to the last.

Similarly, if we omit the start value, then this still works,

a[:6]
# outputs [0, 1, 2, 3, 4, 5]

Now, one last example for you to practice. Guess what does this line do a[:]. Yes, it doesn't have the start and stop index so, remember what their default values are.

And yeah, you got it right. The output will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] coz it means starting with first element to the last element of the list.

Now, let's introduce step size.

a[2:8:2]
# outputs [2, 4, 5, 6]

Here, it means start slice from 2th position till 8th position and increase by value of 2. So, the first element is of course 2, then 2+2=4, then, 4+2=6, then 6+2=8 here we crossed the limit so, stop.

And if you observed we got the even numbers between 2 and 8 position.

Similarly, let me show you how you can reverse a list with extended slicing operation. You can think and try this on your own.

The answer is;

a[::-1]

To, see how this works. The above line means start from the end denoted by -1 then reach to the beginning with step size 1.

Also, one last example for you. Guess the answer what the below line of code does??

a[::-2]

And again you got it right. It means start from end to beginning with step size of 2. So, the answer is [9, 7, 5, 3, 1].

Congrats you now have completely covered the topic of Python range and extended slicing. And now I can guarantee that you will be smiling if someone challenges you to say what a[::-1] does.

Reference Links