-4

Online, I saw a solution like this :

list = [1, 2, 3] 
list_1 = [ i*2 for i in list]

Please, can someone give another way to solve that code? Thank You.

  • 3
    There are effectively infinite, progressively sillier ways to solve this, but this one works just fine, and it's about as canonical and Pythonic as you can get (replacing `i*2` with `i+i` would be equally good, everything else would deteriorate). What's wrong with it? Why do you need a different solution? – ShadowRanger May 03 '20 at 13:26
  • This is probably the fastest and pythonic way to do it. – Austin May 03 '20 at 13:27
  • 1
    I think you will find many way in https://stackoverflow.com/q/35166633/12744275 – Renaud May 03 '20 at 13:29
  • I asked for another solution because I'm a Python's beginner and this expression ( list_1 = [ i*2 for i in list] ) is new for me. – noblegoss Donxiqote May 03 '20 at 13:36
  • @noblegossDonxiqote: The solution is [to learn how list comprehensions work](https://stackoverflow.com/q/34835951/364696), not to avoid a common, useful feature of the language. – ShadowRanger May 03 '20 at 14:04

1 Answers1

1

you can use map

list(map(lambda x: x * 2 , [1,2,3]))

output

[2, 4, 6]
Beny Gj
  • 550
  • 3
  • 14