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
221 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
1 answer

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

Yes it is possible. You can refer ...READ MORE

answered Jun 27, 2019 in Python by Arvind
• 2,980 points
471 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
62 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
26 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,230 points
99 views
0 votes
1 answer
+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 552 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
37 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
47 views