Cannot reshape array of size 1 into shape (3,3)

I have a code that runs on numpy. I already use anaconda and open vscode from there

import numpy as np
def calculate(list):
    if len(list) != 9:
        raise ValueError("Input list must contain exactly 9 elements.")
    return  {
        'mean': [axis1_mean, axis2_mean, flattened_mean],
        'variance': [axis1_v, axis2_v, flattened_v],
        'standard deviation': [axis1_std, axis2_std, flattened_std],
        'max': [axis1_max, axis2_max, flattened_max],
        'min': [axis1_min, axis2_min, flattened_min],
        'sum': [axis1_sum, axis2_sum, flattened_sum]
        }

array_2d = np.array(list).reshape(3,3)

axis1_mean = np.mean(array_2d, axis=0).tolist()
axis2_mean = np.mean(array_2d, axis=1).tolist()
flattened_mean = np.mean(array_2d)
    #variance
axis1_v = np.var(array_2d, axis=0).tolist()
axis2_v = np.var(array_2d, axis=1).tolist()
flattened_v = np.var(array_2d)
    #standard deviation
axis1_std = np.std(array_2d, axis=0).tolist()
axis2_std = np.std(array_2d, axis=1).tolist()
flattened_std= np.std(array_2d)
    #max
axis1_max = np.max(array_2d, axis=0).tolist()
axis2_max = np.max(array_2d, axis=1).tolist()
flattened_max = np.max(array_2d)
    #min
axis1_min = np.min(array_2d, axis=0).tolist()
axis2_min = np.min(array_2d, axis=1).tolist()
flattened_min = np.min(array_2d)
    #sum
axis1_sum = np.sum(array_2d, axis=0).tolist()
axis2_sum = np.sum(array_2d, axis=1).tolist()
flattened_sum = np.sum(array_2d)

list = [9,1,5,3,3,3,2,9,0]
result = calculate(list)
print(result)

I found a few issues. First, the variables is not accessed if I indent it into the calculation (list). Second, when I run this code, it gives me this error line:

array_2d = np.array(list).reshape(3,3)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 1 into shape (3,3)

When I use gitpod to solve this, everything is fine and perfect. But not with the VS Code.

How do I fix this?

Especially the second question.

New contributor

Hartmann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

first, the variable are not accessed if I indent it into calculate(list).

The code from array_2d = ... to flattened_sum = ... should indeed be inside the function definition. However, if you just indented that part of the code, it would never be executed, because it would come after the return statement inside the function. The effect of a return statement is not only to return the specified value, but also to exit the function.
Also, you cannot return values you have not calculated yet, so clearly you want that return statement at the very end of the function body.

second, when I run this code, it gives me this error line array_2d = np.array(list).reshape(3,3) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: cannot reshape array of size 1 into shape (3,3)

When you run the code as given, at the point where the interpreter reaches the line array_2d = np.array(list).reshape(3,3), you have not defined your own list yet. So what is list at that point? It is a built-in Python function! Hence, np.array(list) is interpreted as a NumPy array that contains that function as its one and only item. This explains the error message.

Assigning another object to a Python keyword is usually a bad idea. In your case, your program would not be able to use the list() function anymore, and error messages may become confusing, as you have seen. So to avoid overwriting the built-in function, replace all instances of list in your code with a different name, one that is not a Python keyword.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật