[::-1] in Python with Examples

What is [::-1] in Python?

Python has many different syntax and applications when it comes to utilizing 1. It could either be represented as [-1], or it can also be described as [::-1].

The square brackets in the above notation generally specify more than one item in the list. When using square brackets, one specifies either multiple items or a single item in the list.

Whenever a programmer defines [::-1], it suggests that the program has to traverse from start to end in a given list. You can do indexing in python, which helps to slice and dice an iterable sequence such as a list or string.

What is the type of 1 in Python?

There are two ways to represent 1 in python as described below: –

  • It can be represented in a simple square bracket with no colon and another method with double colon syntax.
  • An iterable sequence such as a list or string can be parsed to item level using [] brackets.
  • Within the [] brackets, one has to provide different indexes.

Following is the syntax for the List as shown below: –

Syntax: –

SEQUENCE [START: STOP: STEP]

The above syntax is a general representation. The syntax for square brackets with no colon is represented as follows:

Syntax: –

SEQUENCE [-1]

The syntax for square brackets with two colon is represented as follows:

Syntax: –

SEQUENCE[::-1]

Explanation: –

  • The start parameter represents the start index number.
  • The stop parameter represents the end or last index number.
  • The parameter step is an optional parameter that provides an increment for a selection. In the syntax, the step count is initialized as -1.
  • A sequence could be a string, list, or dictionary in the above syntax.

Difference between a[-1] and a[::-1] in Python

A [-1] is used for negative indexes and helps select items in reverse order in a given list. It signifies the beginning of the list from the end of the reverse order. Here, [-1] signifies the first item from the last position. The following is an example that explains the usage of A[-1]

Example: –

Python Code:

b= [7,4,5,6]
print ("The list is", b)
print (" The last element in the list is", b [-1])

Output:

The list is [7, 4, 5, 6]
The last element in the list is 6

A[::-1] and A[-1] look similar in terms of execution but are different in terms of syntax. The first syntax has three parameters, whereas the latter syntax has only a single parameter.

The first two parameters present in A[::-1] represent the start and stop for a list. If the start and stop parameters are omitted, it returns all the items in the list.

This last parameter shows the output in reverse order. This parameter is optional.

The following is an example that explains the usage of A[::-1]

Example: –

Python Code:

b= [7,4,5,6]
print ("The list is", b)
print (" The reversed list as determined is", b [::-1])

Output:

The list is [7, 4, 5, 6]
The reversed list as determined is [6, 5, 4, 7]

Explanation:

The above Python code does not produce the same results for the list created. Instead, it provides a reverse list for the base list provided as the input value.

How to perform Indexing in Python?

In order to access an item in an iterable python list, there is a need to perform indexing in the list or use an index as applied with the position of the item present in the list.

Python performs zero-based indexing for lists. In the case of zero-based indexing, the first item in the list is assigned as position 0.

Let’s take an example and try to access the beginning and second items in a python list.

Example: –

Python Code:

b= [7,4,5,6]
print ("The list is", b)
print (" The reverse ordered list as determined is", b [::-1])
print (" The second item found in list is", b [1])
print (" The first or beginning item in the list is", b [0])

Output:

The list is [7, 4, 5, 6]
The reverse ordered list as determined is [6, 5, 4, 7]
The second found item in the list is 4
The first or beginning item in the list is 7

Explanation:

As seen in the above code, to access the first element in the list, 0 was applied as an index within the square of the list type. Similarly, 1 was applied as an index within the list type’s square to access the list’s second element. The indexing of the list can also apply to dictionaries, strings, and tuples.

Let us take an example of the indexing of string type in python.

Example:

Python Code:

b= "GURU99"
print ("The list is", b)
print (" The reverse ordered list as determined is", b [::-1])
print (" The second item found in list is", b [1])
print (" The first or beginning item in the list is", b [0])

Output:

The list is GURU99
The reverse ordered list as determined is 99URUG
The second item found in the list is U
The first or beginning item in the list is G

Explanation:

As seen above, the output shares the first and second elements of string “GURU99”. A string has to be initialized using double quotes. Python also allows us to perform negative indexing of iterable lists, strings, or dictionaries.

By providing a negative index, one can access the last as well as the second last element of the list. Let us take an example of the negative indexing of a string, as shown below.

Example: –

Python Code:

b= "GURU99"
print ("The list is", b)
print (" The reverse ordered list as determined is", b [::-1])
print (" The second item found in list is", b [-2])
print (" The first or beginning item in the list is", b [-1])

Output:

The list is GURU99
The reverse ordered list as determined is 99URUG
The second item found in the list is 9
The first or beginning item in the list is 9

Role of slicing in Python

In simpler words, a slice means to cut something. It allows dicing a list in python.

Here, how slicing is important in Python:

  • It allows access to specific elements of an iterable list or a string.
  • It also helps in deleting and modifying the string or a list.
  • It makes iterable sequence to be concise and readable.
  • It enables us to access multiple items compared to a single element under the indexing concept.
  • It utilizes indexes to procure specific item ranges in the iterable sequence.
  • The indexes under python are always zero-based, whether doing slicing or indexing over an iterable sequence.

Slicing can be achieved in two basic forms.

  • The first form is to provide start and stop index parameters of a sequence. By doing so, the sequence would return all possible items between the start of the sequence and the stop [end -1] of the sequence.
  • The second method is to provide the sequence’s start and stop index parameters along with the step. A step enables the return of specific or selected items within a range of items between the start and stop.

Following is the syntax for the first form of slicing: –

Syntax:

SEQUENCE [START: STOP]

Following is the syntax for the second form of slicing: –

Syntax:

SEQUENCE [START: STOP: STEP]

Let us take the example of the first form of slicing example as shown below.

Example

Python Code:

b= "GURU99"
print ("The list is", b)
Substring=b[0:3]
print (" The substring in the list is", Substring)

Output:

The list is GURU99
The substring in the list is GUR

Let us take the example of the second form of slicing example as shown below.

Example

Python Code:

b= "GURU99"
print ("The list is", b)
Substring=b[0:9:2]
print (" The substring in the list is", Substring)

Output:

The list is GURU99
The substring in the list is GR9

Python also performs negative slicing for an iterable sequence. It enables you to access a range of items or elements from the sequence’s end.

Let us take an example to access the last three elements from the sequence as shown below.

Example

Python Code:

b= "GURU99"
print ("The list is", b)
Substring=b[-5:-1]
print (" The substring in the list is", Substring)

Output:

The list is GURU99
The substring in the list is URU9

How to reverse a Python list using 1 in Python?

The negative slicing and negative indexing can be applied to reverse a string or list in python. Let us take a string named “GURU99” to illustrate an example.

Example

Python Code:

b= "GURU99"
print ("The list is", b)
Substring=b[::-1]
print (" The reverse of string GURU99 is", Substring)

Output:

The list is GURU99
The substring in the list is 99URUG

Explanation:

Here, the code fetches all the string elements from the last position. It starts with -1 and fetches all the items or elements from the last position. The above line of code tells python to step back from the last element and step up to the first element, which results in a reverse list or a string.

Example of 1 in Python

Slicing or indexing can be employed to extract a smaller list from a more extensive list. Similarly, it can be used to extract a substring from a larger string.

Let us take an example of how to use 1 in python to extract a smaller list out of a bigger list:

Example

Python Code:

b = [1, 2, 3, 4, 5, 6, 7]
slist = b[1::]
print("the smaller list from larger list is:",slist)

Output:

the smaller list from the larger list is: [2, 3, 4, 5, 6, 7]

Explanation:

In the above code, leaving the first index, python extracts all elements starting from position 2 of the list and creates a smaller list. This has happened as python follows zero-indexing when applying indexes to the listed elements’ positions.

Summary:

  • Python uses 1 to perform indexing and slicing of lists, strings, and dictionaries.
  • There are three sequence types in python.
  • An iterable sequence can be either list, strings, or dictionaries. These are built-in types of objects.
  • Python supports negative as well as positive indexing.
  • It also supports negative as well as positive slicing.
  • There is a difference in syntax as well as logic between the representation of a[-1] and a[::-1]
  • A[-1] provides the last element of the list.
  • A[::-1] provides all elements starting from the last element of the list. This command helps in reversing an iterable sequence.
  • Python supports a zero-indexing system.