reverse a list without built-in functions

0 votes
How to reverse a list in Python without using built-in functions?
Jul 31 in Python by Fata
• 1,040 points
91 views

1 answer to this question.

0 votes

You can do it in 2 ways:

  1. Using user-defined-functions
  2. Using [::-1] operator

Using user-defined-functions:

EXAMPLE:

def myfunc(a):
    rev_str = []
    for x in a:
        rev_str.insert(0, x)
    return rev_str
print(myfunc([1,2,3,4,5]))

OUTPUT:     

[5, 4, 3, 2, 1]
Using [::-1]:
EXAMPLE:
y=[1,2,3,4,5]
z=y[::-1]
print(z)
OUTPUT:  
[5, 4, 3, 2, 1]
answered Jul 31 by Wajiha
• 1,930 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14 in Python by Junaid
50 views
0 votes
1 answer

Traverse a list in reverse

for item in my_list[::-1]: ...READ MORE

answered May 23, 2018 in Python by Nietzsche's daemon
• 4,260 points
23 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 13,210 points
86 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 401 views
0 votes
1 answer

Slicing a list using a variable, in Python

Yes its possible. Use the following piece ...READ MORE

answered Jul 2 in Python by Wajiha
• 1,930 points
30 views
+1 vote
1 answer

Creating a range of dates in Python

Yes, you can do it as follows: import ...READ MORE

answered Jul 4 in Python by Wajiha
• 1,930 points
39 views