How to build a new list from a list in Python?
Question: I have a python list that contains a set of objects (a class that has it's own
properties and functions and variables). I would like to extract some of the
list elements and create a new list.

My question is: Are the new list elments going to remain same or they will be
considered new instances of the class when I extract them from list?

For example: `List layer = [myclass1, myclass2, myclass3]`

I want new list `layernew = [myclass1]` or any such combination. How can I
extract the items. When I print directly layer and new layer (by using
`newlayer=layer[0:1]`) I see that it gives me the same instance reference of
the object.


Answer: > Are the new list elments going to remain same or they will be considered new
> instances of the class when I extract them from list.

They'll be the same. Demo:

    
    
    class Widget:
        def __init__(self, value):
            self.value = value
    
    a = [Widget(4), Widget(8), Widget(15)]
    b = a[0:1]
    
    print a[0] is b[0]
    

The output is `True`, so `a[0]` and `b[0]` are references to the same object.

One way to change this behavior is to use the `copy` module's `deepcopy`
method. This will attempt to duplicate the object you give it and return a
referentially distinct object with identical values.

    
    
    import copy
    
    class Widget:
        def __init__(self, value):
            self.value = value
    
    a = [Widget(4), Widget(8), Widget(15)]
    b = copy.deepcopy(a[0:1])
    
    print a[0] is b[0]
    #result: False
    

