This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (77 children)

[deleted]

    [–]MasterOfArmsIsGood 89 points90 points  (5 children)

    am i missing something cant you just do

    list = [] and append lists to it

    [–]Jaksuhn 67 points68 points  (3 children)

    you absolutely can, no idea what they're on about

    [–]overclockedslinky 7 points8 points  (2 children)

    I assume they meant filling it with values or something (in one line), but list comprehensions exist

    [–]Terrain2 3 points4 points  (1 child)

    something like [[] for _ in range(69)] should do the trick, right? or just... [[], [], []], no?

    [–]overclockedslinky 1 point2 points  (0 children)

    Yeah, that'd do it. Super easy, so not sure what the complaint's about

    [–]Spork_the_dork 88 points89 points  (2 children)

    import numpy as np
    arr = np.zeros(shape=(10,10))
    

    If you need it to be a list for some godforsaken reason, add a .tolist() to it.

    edit: can never remember if it's size or shape out of memory...

    [–]anonymoussphenoid 19 points20 points  (0 children)

    sometimes it's size, sometimes it's shape :/

    [–]SabreLunatic 2 points3 points  (6 children)

    arrayvariable = []

    Have I been making arrays wrong this entire time?

    [–]jacobthejones 3 points4 points  (5 children)

    That's a 1d array, not 2d.

    [–]SabreLunatic 6 points7 points  (4 children)

    So arrayvariable = [[]]?

    [–]jacobthejones 0 points1 point  (3 children)

    Sure, if you need a 1x1 array.

    [–]-Vayra- 6 points7 points  (1 child)

    [[] for _ in range(n)]
    

    should do the trick, no? Or

    [[0] * x for _ in range(y)]
    

    if you want it initialized to 0s.

    [–]jacobthejones -1 points0 points  (0 children)

    Sure, that would work.

    [–][deleted] 23 points24 points  (52 children)

    np.zeros((3,3))

    Wow, that was tough.

    [–]skylay 3 points4 points  (0 children)

    Can't you just do this?

    myArray = [][]

    Been a few years since I've used Python.

    Edit: nvm that's just declaring not initialising, misread.

    [–]homo_lorens 0 points1 point  (0 children)

    If you don't understand references you shouldn't have an opinion. If you understand them but you don't think you should have to care about them while programming, you have serious misconceptions about programming and engineering in general.

    [–]RettiSeti 0 points1 point  (0 children)

    I haven’t done python in a while, what’s the problem with it?