reverse a list without built-in functions

0 votes
How to reverse a list in Python without using built-in functions?
Jul 31, 2019 in Python by Fata
• 1,050 points
4,811 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, 2019 by Wajiha
• 1,950 points

Related Questions In Python

0 votes
4 answers

how to sort a list of numbers without using built-in functions like min, max or any other function?

I guess you are trying to do ...READ MORE

answered Dec 11, 2020 in Python by Rajiv
• 8,880 points
31,872 views
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, 2019 in Python by Junaid
262 views
0 votes
1 answer
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
110 views
0 votes
2 answers
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,864 views
0 votes
0 answers
0 votes
1 answer

Slicing a list using a variable, in Python

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

answered Jul 2, 2019 in Python by Wajiha
• 1,950 points
286 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, 2019 in Python by Wajiha
• 1,950 points
620 views