02 Python Lists
02 Python Lists
02 Python Lists
Creating List
MyList = []
print("Blank List: ")
print(MyList)
print(len(List1))
Negative indexing
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the
second-last item, etc.
Slicing of a List
In Python List, there are multiple ways to print the whole List with all the elements, but to print a
specific range of elements from the list, we use the Slice operation. Slice operation is performed
on Lists with the use of a colon(:).
• To print elements from beginning to a range use [: Index]
• to print elements from end-use [:-Index]
• to print elements from specific Index till the end use [Index:]
• to print elements within a range, use [Start Index:End Index]
• to print the whole List with the use of slicing operation, use [:]
• to print the whole List in reverse order, use [::-1]
List Methods
Function Description
list1 = ['1','2','3','4']
s = "-"
s = s.join(list1)
print(s)
output: 1-2-3-4
Copy a list
Aliasing Lists
In Python, you might use the = operator copy of an object. It is tempting to think this creates a new
object, but it doesn't. Instead, it creates a new variable that refers to the original object. This means
changing a value in the copied object changes the value of the original object as well.
a = [1, 2, 3]
b=a
b[0] = 100
print(a: ', a)
print(b: ', b)
Output:
a: [100, 2, 3]
b: [100, 2, 3]
As a and b now point to the same list, updating one always updates the other.
To verify that both objects really refer to the same object, you can also use the id() method. Each
Python object has a unique ID. If you check the IDs of the a and b, you can see they are the exact same:
print(id(a))
print(id(b))
Output:
139804802851400
139804802851400
Shallow copy
A shallow copy is shallow because it only copies the object but not its child objects. Instead, the child
objects refer to the original object’s child objects.
import copy
groups = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_groups = copy.copy(groups)
Here, you have groups, which is a list of lists of numbers. And you have a shallow copy called
new_groups. Let’s examine these:
1. The ID of groups is not the same as the ID of new_groups, which is reasonable, as new_groups is a
copy of groups.
print(id(groups), id(new_groups))
Output:
140315092110600 140315092266184
It seems as if new_groups was a completely independent copy of groups. But let’s take it a step further
to see that it’s not.
2. Here is where the shallowness becomes evident. The IDs of the lists in the new_groups are equal to
the IDs of the lists of the original groups:
print(id(groups[0]), id(new_groups[0]))
Output:
140315092110664 140315092110664
The shallowness means only the “outer” list gets copied. But the inner lists still refer to the lists of the
original list. Due to this, changing a number in the copied list affects the original list:
new_groups[0][0] = 100000
print(new_groups[0])
print(groups[0])
Output:
[100000, 2, 3]
[100000, 2, 3]
3. The “outer” list of new_groups is a “real” copy of the original groups. Thus, you can add new
elements to it or even replace existing ones. These changes won’t affect the original groups list.
For example, let’s replace the first list in new_groups with a string. This should not affect groups.
new_groups[0] = "Something else"
print(new_groups)
print(groups)
Output:
Something else
[100000, 2, 3]
Deep copy
A deep copy creates a completely independent copy of the original object.
import copy
groups = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_groups = copy.deepcopy(groups)
print(id(groups), id(new_groups))
Output:
140416138566984 140416138739016
2. The IDs of the lists in new_groups are not equal to the IDs of the lists in groups:
print(id(groups[0]), id(new_groups[0]))
Output:
140416138567048 140416138566728
Changing a number in new_groups doesn’t change that value in the original groups.
new_groups[0][0] = 100000
print(new_groups[0])
print(groups[0])
Output:
[100000, 2, 3]
[1, 2, 3]
3. The new_groups is an independent copy of groups. Thus, there is no way changes made in
new_groups would be visible in the original groups.
A shallow copy means if we modify any of the nested list elements, changes are reflected in both the
list as they point to the same reference.
Whereas in deep copy, when we add an element in any of the lists, only that list is modified.
When we use the “=” operator the new list refers to the same object, hence any change (append,
remove, change of value) in one list is reflected on both.
But when we use the list.copy() method, changes made to one list or not reflected on other except for
in nested elements (like list within a list), Here we should use the copy.deepcopy() from the copy
module to avoid this problem.