blist = alist in python

To assign one list to another in python, you can simply use the assignment operator. Here's an example:

main.py
alist = [1, 2, 3, 4]
blist = alist  # assigning alist to blist
print(blist)  # [1, 2, 3, 4]
92 chars
4 lines

In this example, alist is assigned the list [1, 2, 3, 4]. Then, blist is assigned alist, so blist refers to the same list object as alist.

If you modify the original list (in this case, alist), the changes will also be reflected in the new list (in this case, blist):

main.py
alist.append(5)
print(blist)  # [1, 2, 3, 4, 5]
48 chars
3 lines

In this example, 5 is appended to alist, which also modifies blist. Now, blist is [1, 2, 3, 4, 5].

If you want to create a copy of the original list, you can use the copy() method or slice notation:

main.py
clist = alist.copy()  # creating a copy of alist
dlist = alist[:]  # creating a copy of alist using slice notation
115 chars
3 lines

Both clist and dlist will refer to a new list object that has the same elements as alist.

gistlibby LogSnag