I am trying to square each number in the list under nums. But I don't know how to do it. I got this so far:

def squareEach(nums):
	for i in nums:
		y=i*i
	return y

What is happening here is it's counting how many numbers are in the list and then squaring it.I want the square of each number in the list....please help!thanks!

Dani AI

Generated

's function is overwriting a single variable on each loop and then returning that one value, so you only get the final square instead of a list of squares. and pointed toward building a sequence; below are two alternative patterns that avoid using append and show different trade-offs.

If you want to modify the original list in place (no extra list allocation):

def square_each_inplace(nums):
    for i in range(len(nums)):
        nums[i] = nums[i] * nums[i]
    return nums

This is O(n) time and uses no extra list memory, but it mutates the input list — use it only when that side effect is acceptable.

If you prefer to produce a new list without append, use map (wrap with list() for Python 3):

def square_each_map(nums):
    return list(map(lambda x: x * x, nums))

map returns a list in Python 2 but an iterator in Python 3, so list(...) makes the function behave the same across versions. This keeps the original list unchanged and is concise.

Final notes: decide whether mutation is OK (in-place) or not (return new list). Validate input if there might be non-numeric items, and remember that the core bug in the original post was returning a single scalar rather than accumulating results — moving from “overwrite one variable” to “build or update a sequence” fixes it.

Recommended Answers

All 6 Replies

def squareEach(nums):
    return [i*i for i in nums]

Thanks!:) But what if it's in the arrangement like mine, how would you write that with the same output as yours?

Something like:

def squareEach(nums):
    squares=[]
    for number in nums:
        squares.append(number*number)
    return squares

what if <list>.append(x) is not used, is there any other way to write this code in that arrangement? I'm sorry...I'm new to this and I haven't learned that yet..well now i have but I can't use that yet. thanks!

def squareEach(nums):
    squares=[]
    for number in nums:
        squares += [number * number]
    return squares

what if <list>.append(x) is not used, is there any other way to write this code in that arrangement? I'm sorry...I'm new to this and I haven't learned that yet..well now i have but I can't use that yet. thanks!

You could print them out instead of returning the value or build the result with +, which is not the recommended way. Or you could use recursion.

def squareEach(nums):
    squares=[]
    for number in nums:
        squares+=[number*number]
    return squares

# test it
print zip(range(20),squareEach(range(20)))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.