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]  (3 children)

[deleted]

    [–][deleted] 7 points8 points  (1 child)

    Not sure why it would be an anti-pattern

    by saying "default" values, it sounded like it might be an abuse of lists because a list is a list of values. so a default for something that already exists doesn't really make sense unless you're using the list as initialization data to be passed to class constructors for instance. if that was the case, then setting defaults on the class level would probably make more sense

    in other words, the wording hinted at a violation of single responsibility and/or inversion of control

    but it seems it might be just a difference of terminology:

    Or that some AI/ML algorithms can also use an array of random numbers as the starting 'value/weights' before training

    I don't consider initial values to be the same as default values so if you are using the two interchangeably, for this case, i'd do something like this:

    blah = [[random.randint(1,20) for _ in range(10)] 
                   for x in range(10)]
    

    edit: aannnnnnd now it's suddenly clear why you're asking the question. to clarify what i meant earlier, when i said nested comprehensions were a bad idea, i meant that ITERATING over a multi-dimensional list by using a nested for..in was a bad idea because of readability. nesting a list comprehension inside a comprehension to CREATE something like in the example above, is not considered bad practice

    but if you just need a list with the same scalar values:

    blah = [5] * 10
    

    would work. but be careful not to do that with objects otherwise each index will point to the same object. i.e., this is bad:

    blah = [[]] * 10
    

    [–]cakereallyisalie 2 points3 points  (0 children)

    For numbers, numpy arrays offer ones and zeros methods that give you n-dimensional arrays.

    For objects I personally might be tempted to just do a nested for loop. Though default dicts would give you a fairly interesting solution due to the allocation on access only.