Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:06:57+00:00 2026-05-24T08:06:57+00:00

I wanna create some array in python of array of specified dimension of specific

  • 0

I wanna create some array in python of array of specified dimension of specific type initialized with same value. i can use numpy arrays of specific size but I am not sure how to initialize them with a specific value. Off course I don’t want to use zeros() or ones()

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:06:59+00:00Added an answer on May 24, 2026 at 8:06 am

    There are lots of ways to do this. The first one-liner that occurred to me is tile:

    >>> numpy.tile(2, 25)
    array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
           2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
           2, 2, 2, 2, 2])
    

    You can tile a value in any shape:

    >>> numpy.tile(2, (5, 5))
    array([[2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2]])
    

    However, as a number of answers below indicate, this isn’t the fastest method. It’s designed for tiling arrays of any size, not just single values, so if you really just want to fill an array with a single value, then it’s much faster to allocate the array first, and then use slice assignment:

    >>> a = numpy.empty((5, 5), dtype=int)
    >>> a[:] = 2
    >>> a
    array([[2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2],
           [2, 2, 2, 2, 2]])
    

    According to a few tests I did, there aren’t any faster approaches. However, two of the approaches mentioned in answers below are equally fast: ndarray.fill and numpy.full.

    These tests were all done in ipython, using Python 3.6.1 on a newish mac running OS 10.12.6. Definitions:

    def fill_tile(value, shape):
        return numpy.tile(value, shape)
    
    def fill_assign(value, shape, dtype):
        new = numpy.empty(shape, dtype=dtype)
        new[:] = value
        return new
    
    def fill_fill(value, shape, dtype):
        new = numpy.empty(shape, dtype=dtype)
        new.fill(value)
        return new
    
    def fill_full(value, shape, dtype):
        return numpy.full(shape, value, dtype=dtype)
    
    def fill_plus(value, shape, dtype):
        new = numpy.zeros(shape, dtype=dtype)
        new += value
        return new
    
    def fill_plus_oneline(value, shape, dtype):
        return numpy.zeros(shape, dtype=dtype) + value
    
    for f in [fill_assign, fill_fill, fill_full, fill_plus, fill_plus_oneline]:
        assert (fill_tile(2, (500, 500)) == f(2, (500, 500), int)).all()
    

    tile is indeed quite slow:

    In [3]: %timeit fill_tile(2, (500, 500))
    947 µs ± 10.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    

    Slice assignment ties with ndarray.fill and numpy.full for first place:

    In [4]: %timeit fill_assign(2, (500, 500), int)
    102 µs ± 1.37 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    In [5]: %timeit fill_fill(2, (500, 500), int)
    102 µs ± 1.99 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    In [6]: %timeit fill_full(2, (500, 500), int)
    102 µs ± 1.47 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    

    In-place broadcasted addition is only slightly slower:

    In [7]: %timeit fill_plus(2, (500, 500), int)
    179 µs ± 3.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    

    And non-in-place broadcasted addition is only slightly slower than that:

    In [8]: %timeit fill_plus_oneline(2, (500, 500), int)
    213 µs ± 4.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna create an application in android to install a Hindi Font. Can some
I wanna get the Timedate value from another page using request.querystring and then use
What would be the best way to create an array that can have an
I got some trouble with the TableLayout Android. I wanna create a table with
I wanna to create chat program, messages can be displayed in a different ways,
I have create several canvas with transparent background and wanna make some move event
I am trying to create a canvas in Android where we can use it
I wanna create some loading dots, like this: At 0000 miliseconds the span content
i wanna create a java application that must perform some essential action (closing of
Hi i wanna work with some bluetooth communication using my Android device. How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.