def nest_elements(list1, start_index, stop_index):
    '''
    Create a nested list within list1. Elements in [start_index, stop_index]
    will be in the nested list.
Example:
    >>> x = [1,2,3,4,5,6]
    >>> y = nest_elements(x, 0, 4)
    >>> print(y)
    >>> [[1, 2, 3, 4, 5], 6]
Parameters:
----------
list1 : (list)
    A heterogeneous list.
start_index : (int)
    The index of the first element that should be put into a nested list
    (inclusive).
stop_index : (int)
    The index of the last element that should be put into a nested list
    (inclusive).
Returns:
----------
A copy of list1, with the elements from start_index to stop_index in a
sub_list.
'''
for i in range(len(list1)):
    if i>= start_index or i<= stop_index:
        list1.append(i)
    return list1
pass
            Asked
            
        
        
            Active
            
        
            Viewed 172 times
        
    1
            
            
         
    
    
        Ch3steR
        
- 20,090
- 4
- 28
- 58
 
    
    
        marwah faraj
        
- 33
- 4
- 
                    1I don't understand how you expect this code to solve the problem. In particular: think carefully about what you `.append` to `list1`, and why; and think carefully about what should happen when `i` is outside the range for the `if` condition; and think carefully about how you know when you are done with the process. – Karl Knechtel Nov 29 '20 at 07:04
- 
                    Your provided code does not seem to be complete. It seems to gather the elements that should be in the new sub_list but nothing more. Did you actually try this code. And please be more specific about what you need help with. – Jolbas Nov 29 '20 at 08:30
- 
                    karl knechtel I'm new with python and I still learning so please be easy with me..thanks – marwah faraj Nov 29 '20 at 18:52
- 
                    Jolbas I tried but it did not work and I'm still learning that is why I asked for help – marwah faraj Nov 29 '20 at 18:53
2 Answers
2
            
            
        You can use slice assignment* here.
from copy import deepcopy
def nested(vals, start, end):
    out = deepcopy(vals)
    out[start:end+1] = [out[start:end+1]]
    return out
x = [1,2,3,4,5,6]
out = nested(x, 0, 4)
out
# [[1, 2, 3, 4, 5], 6]
* I attached SO link, since I couldn't find Slice Assignment in python docs
 
    
    
        Ch3steR
        
- 20,090
- 4
- 28
- 58
- 
                    This answer currently does not make a copy of the list (though it would be easy to add in and is def a way cleaner solution than mine) – questionerofdy Nov 29 '20 at 06:51
- 
                    1@questionerofdy ahh true, *slice assignment* is in-place. But we can add `out = deepcopy(vals)` to avoid mutating `x`. Edited the answer. – Ch3steR Nov 29 '20 at 06:58
- 
                    
0
            
            
        >>> def nest_elements(list1, start_index, stop_index):
...     list1[start_index:stop_index+1] = [list1[start_index:stop_index+1]]
...     return list1
... 
>>> print(nest_elements([1,2,3,4,5,6],0,4))
[[1, 2, 3, 4, 5], 6]
 
    
    
        Sandeep Agrawal
        
- 175
- 1
- 8