-6

I am answering the Euler project questions in python and I don't know how to multiply a list by itself I can get a list within the range though

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    Welcome to Stack Overflow! Questions that ask "where do I start?" are typically too broad and are not a good fit for this site. People have their own method for approaching the problem and because of this there cannot be a _correct_ answer. Give a good read over [Where to Start](https://softwareengineering.meta.stackexchange.com/questions/6366/where-to-start/6367#6367) and [edit] your post. Make sure to read [ask] and [mcve] before. – Patrick Artner Feb 13 '19 at 16:41
  • 1
    Questions that ask "please help me" tend to be looking for highly localized guidance, or in some cases, ongoing or private assistance, which is not suited to our Q&A format. It is also rather vague, and is better replaced with a more specific question. Please read [Why is “Can someone help me?” not an actual question?](//meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). – Patrick Artner Feb 13 '19 at 16:44
  • Dupe: https://stackoverflow.com/questions/10271484/how-to-perform-element-wise-multiplication-of-two-lists-in-python – Patrick Artner Feb 13 '19 at 16:46

2 Answers2

-1

Try this:

myList = [0,1,2,3,4,5,6,7,8,9]
newList = []

for item in myList:
    newList.append(pow(item,2))

print (newList)

Returns:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
ncica
  • 7,015
  • 1
  • 15
  • 37
C. Lang
  • 463
  • 4
  • 12
-1

you can use the map function:

list_1 = [1, 2, 3, 4, 5]
list_2 = list(map(lambda x: x**2, list_1))

Output

[1, 4, 9, 16, 25]
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Barbara
  • 1
  • 2
  • 1
    General note: If you need a `lambda` to use `map`, don't use `map`. Just use an equivalent list comprehension or generator expression. `map` is only a win when you have an existing, C implemented mapper function; if the function doesn't meet those criteria `map` is slower. This should really just be `list_2 = [x ** 2 for x in list_1]` or `list_2 = [x * x for x in list_1]`, both of which are shorter, simpler, and faster than `map`+`lambda`. – ShadowRanger Feb 13 '19 at 17:17