While using recursion, list won't add any new elements
up vote
0
down vote
favorite
I am asked to define a function that takes in a list and returns another list, all that while using recursion. However when I run the else command and I print the lst_
, the output shows that on every run the list contains a single element, instead of having the doubles added one by one.Also I try not to use append()
Thoughts?
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_ = lst_ + lst[0:1]
print(lst_)
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
This is the output
[2]
[4]
[6]
[8]
[10]
[12]
[14]
[16]
python list recursion
add a comment |
up vote
0
down vote
favorite
I am asked to define a function that takes in a list and returns another list, all that while using recursion. However when I run the else command and I print the lst_
, the output shows that on every run the list contains a single element, instead of having the doubles added one by one.Also I try not to use append()
Thoughts?
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_ = lst_ + lst[0:1]
print(lst_)
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
This is the output
[2]
[4]
[6]
[8]
[10]
[12]
[14]
[16]
python list recursion
Write exactly what you need. With output and input data.
– Rudolf Morkovskyi
Nov 11 at 11:32
@RudolfMorkovskyi edited
– user10158754
Nov 11 at 11:38
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am asked to define a function that takes in a list and returns another list, all that while using recursion. However when I run the else command and I print the lst_
, the output shows that on every run the list contains a single element, instead of having the doubles added one by one.Also I try not to use append()
Thoughts?
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_ = lst_ + lst[0:1]
print(lst_)
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
This is the output
[2]
[4]
[6]
[8]
[10]
[12]
[14]
[16]
python list recursion
I am asked to define a function that takes in a list and returns another list, all that while using recursion. However when I run the else command and I print the lst_
, the output shows that on every run the list contains a single element, instead of having the doubles added one by one.Also I try not to use append()
Thoughts?
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_ = lst_ + lst[0:1]
print(lst_)
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
This is the output
[2]
[4]
[6]
[8]
[10]
[12]
[14]
[16]
python list recursion
python list recursion
edited Nov 11 at 11:37
asked Nov 11 at 11:26
user10158754
Write exactly what you need. With output and input data.
– Rudolf Morkovskyi
Nov 11 at 11:32
@RudolfMorkovskyi edited
– user10158754
Nov 11 at 11:38
add a comment |
Write exactly what you need. With output and input data.
– Rudolf Morkovskyi
Nov 11 at 11:32
@RudolfMorkovskyi edited
– user10158754
Nov 11 at 11:38
Write exactly what you need. With output and input data.
– Rudolf Morkovskyi
Nov 11 at 11:32
Write exactly what you need. With output and input data.
– Rudolf Morkovskyi
Nov 11 at 11:32
@RudolfMorkovskyi edited
– user10158754
Nov 11 at 11:38
@RudolfMorkovskyi edited
– user10158754
Nov 11 at 11:38
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
If the idea is to return a copy without modifying the original, I would not recommend using the mutable default argument.
Instead,
def double(lst):
if not lst:
return
return [2*lst[0], *double(lst[1:])] # [2*lst[0]] + double(lst[1:])
The recursive case must return a fresh list, and the base case will check for, and return an empty list.
lst1 = double([1,2,3,4,5,6,7,8])
print(lst1)
[2, 4, 6, 8, 10, 12, 14, 16]
If you want to have a little fun, you can attempt a generator-based recursive solution using yield from
(generator delegation):
def double(lst):
if lst:
yield 2*lst[0]
yield from double(lst[1:])
lst = list(double([1,2,3,4,5,6,7,8]) )
print(lst)
[2, 4, 6, 8, 10, 12, 14, 16]
add a comment |
up vote
0
down vote
Use .append()
on list to add elements at the end:
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] += lst[0]
lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
# [2, 4, 6, 8, 10, 12, 14, 16]
Also, note that this line lst[0] = int(lst[0]) + int(lst[0])
in you code can be shortened to lst[0] += lst[0]
, because you are dealing with integers only and that explicit casting is redundant.
add a comment |
up vote
0
down vote
If you don't want to use append(). Then you can use this solution:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
return [lst[0] * 2 , *double(lst[1:])]
print(double([1,2,3,4,5,6,7,8]))
The output will be : [2, 4, 6, 8, 10, 12, 14, 16]
Just in case you are wondering with the *double(lst[1:]) call: * is used for unpacking argument list. Read more here.
If you call without the * you will get an output like:
[2, [4, [6, [8, [10, [12, [14, [16, ]]]]]]]]
Another easy solution will be:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
lst[0] = lst[0] * 2
lst_ = lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
add a comment |
up vote
0
down vote
Try this way:
def double(lst, lst_ = ):
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_.extend(lst[:1])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
#=> [2, 4, 6, 8, 10, 12, 14, 16]
Would it be possible to do this without usingappend()
?
– user10158754
Nov 11 at 11:34
Thisfor item in lst[0:1]:
is unnecessary. You can directly add to list.
– Austin
Nov 11 at 11:35
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If the idea is to return a copy without modifying the original, I would not recommend using the mutable default argument.
Instead,
def double(lst):
if not lst:
return
return [2*lst[0], *double(lst[1:])] # [2*lst[0]] + double(lst[1:])
The recursive case must return a fresh list, and the base case will check for, and return an empty list.
lst1 = double([1,2,3,4,5,6,7,8])
print(lst1)
[2, 4, 6, 8, 10, 12, 14, 16]
If you want to have a little fun, you can attempt a generator-based recursive solution using yield from
(generator delegation):
def double(lst):
if lst:
yield 2*lst[0]
yield from double(lst[1:])
lst = list(double([1,2,3,4,5,6,7,8]) )
print(lst)
[2, 4, 6, 8, 10, 12, 14, 16]
add a comment |
up vote
1
down vote
accepted
If the idea is to return a copy without modifying the original, I would not recommend using the mutable default argument.
Instead,
def double(lst):
if not lst:
return
return [2*lst[0], *double(lst[1:])] # [2*lst[0]] + double(lst[1:])
The recursive case must return a fresh list, and the base case will check for, and return an empty list.
lst1 = double([1,2,3,4,5,6,7,8])
print(lst1)
[2, 4, 6, 8, 10, 12, 14, 16]
If you want to have a little fun, you can attempt a generator-based recursive solution using yield from
(generator delegation):
def double(lst):
if lst:
yield 2*lst[0]
yield from double(lst[1:])
lst = list(double([1,2,3,4,5,6,7,8]) )
print(lst)
[2, 4, 6, 8, 10, 12, 14, 16]
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If the idea is to return a copy without modifying the original, I would not recommend using the mutable default argument.
Instead,
def double(lst):
if not lst:
return
return [2*lst[0], *double(lst[1:])] # [2*lst[0]] + double(lst[1:])
The recursive case must return a fresh list, and the base case will check for, and return an empty list.
lst1 = double([1,2,3,4,5,6,7,8])
print(lst1)
[2, 4, 6, 8, 10, 12, 14, 16]
If you want to have a little fun, you can attempt a generator-based recursive solution using yield from
(generator delegation):
def double(lst):
if lst:
yield 2*lst[0]
yield from double(lst[1:])
lst = list(double([1,2,3,4,5,6,7,8]) )
print(lst)
[2, 4, 6, 8, 10, 12, 14, 16]
If the idea is to return a copy without modifying the original, I would not recommend using the mutable default argument.
Instead,
def double(lst):
if not lst:
return
return [2*lst[0], *double(lst[1:])] # [2*lst[0]] + double(lst[1:])
The recursive case must return a fresh list, and the base case will check for, and return an empty list.
lst1 = double([1,2,3,4,5,6,7,8])
print(lst1)
[2, 4, 6, 8, 10, 12, 14, 16]
If you want to have a little fun, you can attempt a generator-based recursive solution using yield from
(generator delegation):
def double(lst):
if lst:
yield 2*lst[0]
yield from double(lst[1:])
lst = list(double([1,2,3,4,5,6,7,8]) )
print(lst)
[2, 4, 6, 8, 10, 12, 14, 16]
edited Nov 11 at 11:42
answered Nov 11 at 11:36
coldspeed
111k17101170
111k17101170
add a comment |
add a comment |
up vote
0
down vote
Use .append()
on list to add elements at the end:
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] += lst[0]
lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
# [2, 4, 6, 8, 10, 12, 14, 16]
Also, note that this line lst[0] = int(lst[0]) + int(lst[0])
in you code can be shortened to lst[0] += lst[0]
, because you are dealing with integers only and that explicit casting is redundant.
add a comment |
up vote
0
down vote
Use .append()
on list to add elements at the end:
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] += lst[0]
lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
# [2, 4, 6, 8, 10, 12, 14, 16]
Also, note that this line lst[0] = int(lst[0]) + int(lst[0])
in you code can be shortened to lst[0] += lst[0]
, because you are dealing with integers only and that explicit casting is redundant.
add a comment |
up vote
0
down vote
up vote
0
down vote
Use .append()
on list to add elements at the end:
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] += lst[0]
lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
# [2, 4, 6, 8, 10, 12, 14, 16]
Also, note that this line lst[0] = int(lst[0]) + int(lst[0])
in you code can be shortened to lst[0] += lst[0]
, because you are dealing with integers only and that explicit casting is redundant.
Use .append()
on list to add elements at the end:
def double(lst, lst_ = ):
"""
parameters : lst of type list;
returns : another list with lst's elements doubled
"""
if len(lst) == 0:
return lst_
else:
lst[0] += lst[0]
lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
# [2, 4, 6, 8, 10, 12, 14, 16]
Also, note that this line lst[0] = int(lst[0]) + int(lst[0])
in you code can be shortened to lst[0] += lst[0]
, because you are dealing with integers only and that explicit casting is redundant.
edited Nov 11 at 11:38
answered Nov 11 at 11:33
Austin
8,8443828
8,8443828
add a comment |
add a comment |
up vote
0
down vote
If you don't want to use append(). Then you can use this solution:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
return [lst[0] * 2 , *double(lst[1:])]
print(double([1,2,3,4,5,6,7,8]))
The output will be : [2, 4, 6, 8, 10, 12, 14, 16]
Just in case you are wondering with the *double(lst[1:]) call: * is used for unpacking argument list. Read more here.
If you call without the * you will get an output like:
[2, [4, [6, [8, [10, [12, [14, [16, ]]]]]]]]
Another easy solution will be:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
lst[0] = lst[0] * 2
lst_ = lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
add a comment |
up vote
0
down vote
If you don't want to use append(). Then you can use this solution:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
return [lst[0] * 2 , *double(lst[1:])]
print(double([1,2,3,4,5,6,7,8]))
The output will be : [2, 4, 6, 8, 10, 12, 14, 16]
Just in case you are wondering with the *double(lst[1:]) call: * is used for unpacking argument list. Read more here.
If you call without the * you will get an output like:
[2, [4, [6, [8, [10, [12, [14, [16, ]]]]]]]]
Another easy solution will be:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
lst[0] = lst[0] * 2
lst_ = lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
add a comment |
up vote
0
down vote
up vote
0
down vote
If you don't want to use append(). Then you can use this solution:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
return [lst[0] * 2 , *double(lst[1:])]
print(double([1,2,3,4,5,6,7,8]))
The output will be : [2, 4, 6, 8, 10, 12, 14, 16]
Just in case you are wondering with the *double(lst[1:]) call: * is used for unpacking argument list. Read more here.
If you call without the * you will get an output like:
[2, [4, [6, [8, [10, [12, [14, [16, ]]]]]]]]
Another easy solution will be:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
lst[0] = lst[0] * 2
lst_ = lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
If you don't want to use append(). Then you can use this solution:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
return [lst[0] * 2 , *double(lst[1:])]
print(double([1,2,3,4,5,6,7,8]))
The output will be : [2, 4, 6, 8, 10, 12, 14, 16]
Just in case you are wondering with the *double(lst[1:]) call: * is used for unpacking argument list. Read more here.
If you call without the * you will get an output like:
[2, [4, [6, [8, [10, [12, [14, [16, ]]]]]]]]
Another easy solution will be:
def double(lst, lst_ = ):
if not lst:
return lst_
else:
lst[0] = lst[0] * 2
lst_ = lst_.append(lst[0])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
answered Nov 11 at 12:10
Anurag
80111
80111
add a comment |
add a comment |
up vote
0
down vote
Try this way:
def double(lst, lst_ = ):
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_.extend(lst[:1])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
#=> [2, 4, 6, 8, 10, 12, 14, 16]
Would it be possible to do this without usingappend()
?
– user10158754
Nov 11 at 11:34
Thisfor item in lst[0:1]:
is unnecessary. You can directly add to list.
– Austin
Nov 11 at 11:35
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
add a comment |
up vote
0
down vote
Try this way:
def double(lst, lst_ = ):
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_.extend(lst[:1])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
#=> [2, 4, 6, 8, 10, 12, 14, 16]
Would it be possible to do this without usingappend()
?
– user10158754
Nov 11 at 11:34
Thisfor item in lst[0:1]:
is unnecessary. You can directly add to list.
– Austin
Nov 11 at 11:35
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this way:
def double(lst, lst_ = ):
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_.extend(lst[:1])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
#=> [2, 4, 6, 8, 10, 12, 14, 16]
Try this way:
def double(lst, lst_ = ):
if len(lst) == 0:
return lst_
else:
lst[0] = int(lst[0]) + int(lst[0])
lst_.extend(lst[:1])
return double(lst[1:])
print(double([1,2,3,4,5,6,7,8]))
#=> [2, 4, 6, 8, 10, 12, 14, 16]
edited Nov 11 at 12:40
answered Nov 11 at 11:33
iGian
2,5892621
2,5892621
Would it be possible to do this without usingappend()
?
– user10158754
Nov 11 at 11:34
Thisfor item in lst[0:1]:
is unnecessary. You can directly add to list.
– Austin
Nov 11 at 11:35
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
add a comment |
Would it be possible to do this without usingappend()
?
– user10158754
Nov 11 at 11:34
Thisfor item in lst[0:1]:
is unnecessary. You can directly add to list.
– Austin
Nov 11 at 11:35
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
Would it be possible to do this without using
append()
?– user10158754
Nov 11 at 11:34
Would it be possible to do this without using
append()
?– user10158754
Nov 11 at 11:34
This
for item in lst[0:1]:
is unnecessary. You can directly add to list.– Austin
Nov 11 at 11:35
This
for item in lst[0:1]:
is unnecessary. You can directly add to list.– Austin
Nov 11 at 11:35
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
@Austin Yup, thanks.
– iGian
Nov 11 at 12:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248247%2fwhile-using-recursion-list-wont-add-any-new-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Write exactly what you need. With output and input data.
– Rudolf Morkovskyi
Nov 11 at 11:32
@RudolfMorkovskyi edited
– user10158754
Nov 11 at 11:38