Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Get map() to Return a List in Python 3.x

#1
How to Get map() to Return a List in Python 3.x

Recap: Python’s map() function takes a function and an iterable—such as a list—as arguments. It then applies the function to each element in the iterable.

It returns a map object. ?

But what if you don’t need a map object but a list?

Old: In Python 2.x this was easy: the map() function simply returned a list rather than an iterable map object.

# Python 2.X
print(map(lambda x: x+1, [1, 2, 3]))
# Output: [2, 3, 4]

New: But if we try this in Python 3.x, the output is not as pretty:

print(map(lambda x: x+1, [1, 2, 3]))
# Output: <map object at 0x0000013CE75DB4E0>

Bah!

Let’s fix this!

Solution: list(map(...))

To get Python’s built-in map() function to return a list in Python 3.x, pass the map object into the list() constructor to convert the iterable map object into a list. For example, to convert the map object created by map(lambda x: x+1, [1, 2, 3]) to a Python list, use list(map(lambda x: x+1, [1, 2, 3])).

print(list(map(lambda x: x+1, [1, 2, 3])))
# Output: [2, 3, 4]

If you have any further question, feel free to join our email academy and ask!

The post How to Get map() to Return a List in Python 3.x first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...ython-3-x/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Compare Rpm Versions in Python? xSicKxBot 0 13 03-01-2021, 07:10 PM
Last Post: xSicKxBot
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 25 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 41 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 33 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 40 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 35 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 39 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] How to Check if an Object is of Type List in Python? xSicKxBot 0 33 02-09-2021, 08:05 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 45 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 38 02-03-2021, 01:31 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] Best LLC Services for Your Freelan...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 1
(Indie Deal) PAW Patrol, Ben10, Dead in ...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
(Free Game Key) Wargame: Red Dragon - Fr...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
Flutter 2 Release Adds Desktop and Web S...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 3
Microsoft - Meet the 2021 Imagine Cup Wo...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
News - Review: G-Darius HD – A True Shmu...
Last Post: xSicKxBot
Yesterday 07:37 PM
» Replies: 0
» Views: 1
News - Minute Of Islands, The Adventure ...
Last Post: xSicKxBot
Yesterday 07:37 PM
» Replies: 0
» Views: 1
News - Tales From the (Deep Stone) Crypt
Last Post: xSicKxBot
Yesterday 10:30 AM
» Replies: 0
» Views: 6
Xbox Wire - The Ford F-150 RLE Has Arriv...
Last Post: xSicKxBot
Yesterday 10:29 AM
» Replies: 0
» Views: 6
News - Don’t Miss: Designing for seasons...
Last Post: xSicKxBot
Yesterday 10:29 AM
» Replies: 0
» Views: 13

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020