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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:34:24+00:00 2026-06-12T03:34:24+00:00

I am told to Write a function, square(a), that takes an array, a, of

  • 0

I am told to

Write a function, square(a), that takes an array, a, of numbers and returns an array containing each of the values of a squared.

At first, I had

def square(a):
    for i in a: print i**2

But this does not work since I’m printing, and not returning like I was asked.
So I tried

    def square(a):
        for i in a: return i**2

But this only squares the last number of my array. How can I get it to square the whole list?

  • 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-06-12T03:34:26+00:00Added an answer on June 12, 2026 at 3:34 am

    You could use a list comprehension:

    def square(list):
        return [i ** 2 for i in list]
    

    Or you could map it:

    def square(list):
        return map(lambda x: x ** 2, list)
    

    Or you could use a generator. It won’t return a list, but you can still iterate through it, and since you don’t have to allocate an entire new list, it is possibly more space-efficient than the other options:

    def square(list):
        for i in list:
            yield i ** 2
    

    Or you can do the boring old for-loop, though this is not as idiomatic as some Python programmers would prefer:

    def square(list):
        ret = []
        for i in list:
            ret.append(i ** 2)
        return ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that takes two string arguments and returns the
I was told that a function defined as static in one .c file is
I am trying to write a function that can check whether or not some
So basically I want to write a function that can be written like this:
In my team I've been told to write resource class like this style: class
I'm trying to write a program in Python and I'm told to run an
Someone told me that openGL is for graphic only, and that it's very bad
I was told that using break and continue labels in an OOP language is
A consultant told me recently that he has clients using IIS. Those servers accept
I was told that Respond.Redirect is an expensive process because it raises a ThreadAbortException.

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.