Try this:

x = [1,1,1,2,1,1]
b = 2

try:
    y = x[:x.index(b)]
except ValueError:
    y = x[:]

For example:

In [10]: x = [1,1,1,2,1,1]
    ...: b = 2
    ...: 
    ...: try:
    ...:     y = x[:x.index(b)]
    ...: except ValueError:
    ...:     # b was not found in x. Just copy the whole thing.
    ...:     y = x[:]
    ...:

In [11]: y
Out[11]: [1, 1, 1]

See list.index() and the shallow-copy slice for more information.

Answer from Will on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ array-copying-in-python
Array Copying in Python - GeeksforGeeks
April 30, 2025 - Explanation: A shallow copy creates a new object, but it shares references to the original array's elements. Using view(), b is a shallow copy of a, meaning changes in a will reflect in b.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_copy_arrays.htm
Python - Copy Arrays
In Python, copying an array refers to the process of creating a new array that contains all the elements of the original array. This operation can be done using assignment operator (=) and deepcopy() method.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.copy.html
numpy.copy โ€” NumPy v2.4 Manual
โ€˜Kโ€™ means match the layout of a as closely as possible. (Note that this function and ndarray.copy are very similar, but have different default values for their order= arguments.) ... If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (defaults to False).
๐ŸŒ
Aims
python.aims.ac.za โ€บ pages โ€บ cp_mutable_objs.html
21. Be careful: copying arrays, lists (and more) โ€” AIMS Python 0.7 documentation
In particular, using the copy.deepcopy method is generally safe for these kinds of objects: import copy A = <some array, list or other mutable object> B = copy.deepcopy( A ) In our own thought-processing and algorithm generation, we might picture the variable A as representing the whole ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-numpy-duplicate-copy-array
Python Numpy - Duplicate or Copy Array Data to Another Array
The array arr is initialized as a 3D numpy array with shape (2, 2, 2). We create a copy of arr using the copy() function and assign it to arr_copy.
Find elsewhere
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ how-to-copy-numpy-array-into-part-of-another-array.aspx
Python - How to copy NumPy array into part of another array?
# Import numpy import numpy as np # Creating two numpy arrays arr1 = np.array([[10, 20, 30],[1,2,3],[4,5,6]]) arr2 = np.zeros((6,6)) # Display original arrays print("Original Array 1:\n",arr1,"\n") print("Original Array 2:\n",arr2,"\n") # Fixing smaller array into larger array # at some specific position arr2[1:4, 1:4] = arr1 # Display result print("Result:\n",arr2) ... Comments and Discussions! ... D.S. Programs ... Copyright ยฉ 2025 www.includehelp.com.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python โ€“ numpy array copy
Python - NumPy Array Copy - Spark By {Examples}
March 27, 2024 - Use numpy.copy() function to copy Python NumPy array (ndarray) to another array. This method takes the array you wanted to copy as an argument and returns
๐ŸŒ
Linux Hint
linuxhint.com โ€บ copy-array-python
Copy Array in Python โ€“ Linux Hint
Using the copy() function is another way of copying an array in Python. In this case, a new array object is created from the original array and this type of copy is called deep copy. If any value is modified in the original or copied array, then it does not create any change on another array.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ python_numpy_copy_part_of_array.php
Python numpy copy part of array
Explanation: When array_view was ... ensure you get an independent duplicate, you should always follow your slicing or indexing operation with the .copy() method....
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-148.php
Python NumPy: Copy data from a given array to another array - w3resource
August 29, 2025 - The colon : in y[:] represents ... โ€˜yโ€™. ... Write a NumPy program to create an exact copy of a given array using the copy() method and verify independence....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.copies.html
Copies and views โ€” NumPy v2.4 Manual
Changes made to the copy do not reflect on the original array. Making a copy is slower and memory-consuming but sometimes necessary. A copy can be forced by using ndarray.copy. ... Views are created when elements can be addressed with offsets and strides in the original array.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_copy_vs_view.asp
NumPy Array Copy vs View
The copy returns None. The view returns the original array. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndarray.copy.html
numpy.ndarray.copy โ€” NumPy v2.4 Manual
January 31, 2021 - >>> import copy >>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> c = copy.deepcopy(a) >>> c[2][0] = 10 >>> c array([1, 'm', list([10, 3, 4])], dtype=object) >>> a array([1, 'm', list([2, 3, 4])], dtype=object)
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to copy a numpy array into another array?
How To Copy a Numpy Array Into Another Array? - AskPython
February 16, 2023 - An array is a type of data structure in Python that stores objects of similar data types. It is almost similar to lists except for the fact that lists can store objects of multiple data types. ... So letโ€™s just right away look at the methods or functions you can use. This in-built function will return the exact same cop of the input array.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ list โ€บ copy
Python List copy()
# copy list using = new_list = old_list # add an element to list new_list.append('a') print('New List:', new_list) print('Old List:', old_list)
๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-make-a-copy-of-an-array-in-Python.php
How to Make a Copy of an Array in Python
Using the Python copy() function, we can make a copy of an array and the copied array will contain all of the same exact elements as the original array.