Python Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and Min

Welcome to the second part of lists. In the last chapter you learnt about basic functions of lists. In this chapter we learnt about some basic operations that can be performed on lists. Here, we will have a look at some more interesting ways of working with lists. So, you now know that index tells you about the position of an element in a list. So, lst1[0] will provide you access to the first element of a list.

Negative Index to a Python List

Now, here is something to think about. What do you think will be the output of following piece of code?

lst1 = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
print(lst1[-2])

Download the code Run the code
Confused? Well, the answer is “Alex”, this is because when you give a negative index number Python counts the element from the right. The rightmost element is at the index of -1. Few other programming languages have this negative index, but this feature is extremely useful. In any other language that does not support negative indexing, one will have to write

lst [lst.length-2]

to achieve the same effect. Python being a beautiful language, provides this negative indexing concept which makes the programmers write less and the code more readable.

Slicing Python Lists

Slicing lists helps in fetching sections (slices) of the list. This is a very useful command to get a partial list from a parent list.

lst [start:end] # Items from index=start to index=end-1
lst [start:]    # Items index=start through the rest of the array
lst [:end]      # All items from the beginning through index=end-1
lst [:]         # A copy of the whole array

There is also a step parameter that you can provide. Mentioning the step parameter will pick only those elements that are at that step. This is useful when you want to skip few inner elements that match the stepping pattern.

lst [start:end:step] # Items from index=start to index=end-1 with a step
lst = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
print(lst[2:4])

Download the code Run the code

The above code will have the output of : [‘Ashok’, ‘Vijay’] since ‘Ashok’ is at index 2 and ‘Vijay’ is at index 4-1.
Now, Try this:

lst = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
print (lst[1:])
print (lst[0:])
print (lst[2:-2]) # all elements starting from third element but skips the last two elements.
print (lst[::2])  # this will print all alternate elements (begin to end in steps of 2)
print (lst[::-1]) # this will print all elements in reverse order

Download the code Run the code

The first print command will skip the first element and print the entire list whereas the second print command to print lst1[0:] will print the entire list as it is.

Combining slicing with negative index

The third print combines slicing with a negative index. The output of this third print statement will print all elements starting from third element but skips the last two elements.

Using a step parameter in the slice function

The fourth print combines slicing with a step. The output of this third print statement will print all elements starting from beginning element to the last element in a step of 2. Basically the all alternate elements of the list.

Reversing a list using a negative step in the slice command

The fifth print combines slicing with a negative step. The output of this third print statement will print all elements starting from ending element to the beginning element. Basically the reverse of the list.

This stackoverflow discussion is very useful if you like to understand more about slicing. Also this python doc has the official definition.

Compare two Lists

Python 2.7 had a function cmp() for comparing two lists which is not a part of Python 3. Hence if you try to use this function the interpreter will display an error. You can however, still compare two lists using ‘==’ operator. Look at code below:

lst1 = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
lst2 = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
lst3 = ['Ajay', 'Bobby','Ashok']
print('list1 is : '+str(lst1))
print('list2 is : '+str(lst2))
print('list3 is : '+str(lst3))
print('Is list 1 equal to list 2?')
print(lst1==lst2);
print('Is list 2 equal to list 3?')
print(lst2==lst3);

Download the code Run the code
lst1 = [‘Ajay’, ‘Bobby’,’Ashok’, ‘Vijay’, ‘Anil’, ‘Rahul’,’Alex’, ‘Christopher’]

Maximum Value of the list

The maximum value in the list can be retrieved by max() function and the minimum value can be retrieved with the help of min() value.

lst1=[10, 294, 1290, 467, 783, 87]
print('list 1 : '+ str(lst1))
print('The maximum value in the list is : ')
print(max(lst1))
print('The minimum value in the list is : ')
print(min(lst1))

Download the code Run the code

<<< Python Lists – basics: define, access, assign, delete, print, concatenateComparison Operators >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .