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 6674791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:46:21+00:00 2026-05-26T03:46:21+00:00

# Method one array_a = [] a = {} for i in range(5): a

  • 0
# Method one
array_a = []
a = {}
for i in range(5):
    a = {}
    a[str(i)] = i
    array_a.append(a)
print(array_a)
# [{'0': 0}, {'1': 1}, {'2': 2}, {'3': 3}, {'4': 4}]

# Method two    
from copy import deepcopy
array_b = []
b = {}
for i in range(5):
    b.clear()
    b[str(i)] = i
    array_b.append(deepcopy(b))
print(array_b)
# [{'0': 0}, {'1': 1}, {'2': 2}, {'3': 3}, {'4': 4}]

I would like to know which one of above is more efficient.
And, if you have a better one, please let me know.

  • 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-26T03:46:21+00:00Added an answer on May 26, 2026 at 3:46 am

    The difference is not relevant. Both need to create a new dict each time. Since the first is clearer, it is preferable over the second method.

    My suggestion would be a list comprehension:

    array_c = [{str(i): i} for i in range(5)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is one method that monkey patched the Dir[] method from autotest class Dir
I don't understand why one method would work and the other would throw a
I am searching for a method to intersect my array collections. I have one
I know how to do the toString method for one dimensional arrays of strings,
One method which I can think of is to reverse the list and then
I have one method that opens a file and passes off to another function
I have one method in my managed bean which returns javascript as a string.
Is there one method or property in the .NET Framework that tells if an
WCF service has one method ( Let's say TestMethod) in which I try to
I'm trying to pass one method to another in elisp, and then have that

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.