Home > Enterprise >  reverse list in the python
reverse list in the python

Time:09-10

I'm trying to reverse a integer and using the below code but the resultant list value in None. The code:

int_a= [1,2,3,4,5,6]
rev_word = int_a.split()
rev_number = rev_number.reverse()
rev_number= "".join(rev)

it return the TypeError. Why?

CodePudding user response:

Here is the short and optimal way to reverse a list in python

int_a= [1,2,3,4,5,6]
int_a = int_a[::-1] 

I hope this will help you.

CodePudding user response:

‍‍reverse() method, reverses list inplace. This means that rev_number.reverse() returns None.

So you can have a reversed list in this way:

int_a= "This is integer"
rev_word = int_a.split()
rev_word.reverse()
rev_number= "".join(rev_word)

CodePudding user response:

int_a = [1,2,3,4,5,6] int-a = int_a[::-1]

this answer is a mark for best answer for reverse list in the python.

  • Related