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
Asked
Active
Viewed 59 times
-6
-
1Welcome 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
-
1Questions 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 Answers
-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
-
1General 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