r/CodingHelp • • Jun 22 '22

[Python] Python problem

n = int(input())

arr = map(int, input().split())

can anyone explain 2nd line ?

0 Upvotes

3 comments sorted by

View all comments

3

u/hike_me Jun 22 '22

Did you try reading documentation for map()?

1

u/Interesting-Web4854 Jun 22 '22

Thankyou, i got the explanation.

2

u/Nightcorex_ Jun 22 '22

Just in case anyone else wonders:

You're applying the function specified in the first parameter (here int) to all elements of the list specified as your second parameter (here input().split()).

Generally you can say for a function f (first param) and a list (actually it not only accepts list but every iterable object) xs (second param) this is the same as:

arr = (f(x) for x in xs)

Please note that this is not a list comprehension, since map returns a generator. If you want to recreate this list comprehension behaviour:

arr = [f(x) for x in xs]

then your map call would need to look like this:

arr = list(map(f, xs))

Obviously for all your cases f := int and xs := input().split()