• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Using Python to Initialize Array of Size N

Using Python to Initialize Array of Size N

August 12, 2022 Leave a Comment

To initialize a list of size N in Python, the easiest way is to use * to multiply a single item list by N.

n = 100

list_of_size_n = [0] * n
list_of_size_n = [None] * n

When working with numbers in a Python program, it’s possible you want to initialize an array of size N in Python.

Arrays in Python are called lists, and we can easily create a list of size N in our Python code.

To initialize a list of size N in Python, the easiest way is to use * to multiply a single item list by N.

Depending on what you are going to do with this list, whether it is going to be filled later in your program or used as placeholders, you can fill it with 0’s or None’s.

Below shows you how to create a list of size N in Python.

n = 100

list_of_size_n = [0] * n
list_of_size_n = [None] * n

How to Create List from 1 to N in Python

When working with numbers in a Python program, it’s possible you want to create an array from 1 to n in Python.

We can easily create a list of the numbers 1 to n in our Python code.

The range() function takes in 3 arguments. The first is the starting point, the second is the ending point, and the third argument is the step size.

For example, if I want all the numbers between 1 and 10, I’d call the range function in the following way.

numbers_1_to_10 = list(range(1,11))

We can define a function which will create a list from 1 to N. This will also give you a list of size N.

Below is an example of a function in Python which returns a list with numbers from 1 to N.

def listFrom1toN(n):
    return list(range(1,n+1))

print(listFrom1toN(13))

#Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

Hopefully this article has been useful for you to learn how to initialize an array of size N in Python.

Other Articles You'll Also Like:

  • 1.  Drop Last n Rows of pandas DataFrame
  • 2.  Drop Last Row of pandas DataFrame
  • 3.  Using Python To Split String by Comma into List
  • 4.  Return Multiple Values from Function in Python
  • 5.  PROC FREQ Equivalent in Python
  • 6.  Remove Brackets from String Using Python
  • 7.  Sort a List of Strings Using Python
  • 8.  Create List of Odd Numbers in Range with Python
  • 9.  pandas to_pickle – Write DataFrame to Pickle File
  • 10.  Transpose DataFrame pandas – Using the pandas transpose Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy