About 285,000 results
Open links in new tab
  1. Create an empty list with certain size in Python - Stack Overflow

    To create a list of size 10(let's say), you can first create an empty array, like np.empty(10) and then convert it to list using arrayName.tolist(). Alternately, you can chain them as well. Alternately, you can chain them as well.

  2. python - How to create a list of empty lists - Stack Overflow

    Nov 30, 2015 · After hours of struggle want to note that apparently comprehension for creation of a list of lists [[] for _ in range(N)] behaves differently creation of list of empty strings ["" for _ in range(N)]. –

  3. performance - Creating an empty list in Python - Stack Overflow

    % python -mtimeit "l=[]" 10000000 loops, best of 3: 0.0711 usec per loop % python -mtimeit "l=list()" 1000000 loops, best of 3: 0.297 usec per loop However, in practice, this initialization is most likely an extremely small part of your program, …

  4. How do I get an empty list of any size in Python?

    It is also possible to create an empty array with a certain size: array = [[] for _ in range(n)] # n equal to your desired size array[0].append(5) # it appends 5 to an empty list, then array[0] is [5] if you define it as array = [] * n then if you modify one item, all are changed the same way, because of …

  5. How to initialize a dict with keys from a list and empty value in …

    If you would add something to the list corresponding to the first key, then it would also be added to the lists belonging to the other keys; because they all reference the same list. – Mathy Commented Nov 4, 2023 at 11:42

  6. python - How to empty a list? - Stack Overflow

    This actually removes the contents from the list, but doesn't replace the old label with a new empty list: del lst[:] Here's an example: lst1 = [1, 2, 3] lst2 = lst1 del lst1[:] print(lst2) For the sake of completeness, the slice assignment has the same effect: lst[:] = []

  7. python - Creating an empty Pandas DataFrame, and then filling it ...

    pd.DataFrame converts the list of rows (where each row is a scalar value) into a DataFrame. If your function yields DataFrames instead, call pd.concat. Pros of this approach: It is always cheaper to append to a list and create a DataFrame in one go than it is to create an empty DataFrame (or one of NaNs) and append to it over and over again.

  8. python - How to fill an empty list with values from user input?

    Sep 8, 2019 · Okay, so I am at ground level in python, currently trying to do the following: I want to create a list of numbers received from user input, so for example I have an empty list and when prompted I t...

  9. python - Creating an empty list to have data assigned afterwards ...

    Dec 7, 2016 · An empty list doesn't have any index yet, it's an empty list! If you want to fill it, you have to do something like this : while count < 10: nameList.append(raw_input("Enter a name: ")) count += 1 This way, your condition will be evaluated and with the append method, you will be able to add new items in your list.

  10. python - Efficient way to either create a list, or append to it if one ...

    Granted that you have a dict with empty (None) keys, it means you also create the dict keys somewhere. You can do so with the dict.fromkeys method, and this method also allows for setting a default value to all keys.

Refresh