A
A
Andrey Godunko2021-12-04 14:12:59
Python
Andrey Godunko, 2021-12-04 14:12:59

What is the correct way to use the map() function?

In my mind, I couldn't figure out how. Please help, I will be grateful
---------------------------------------------------- -------------------------------------------------- -------------
I need to take numbers from a file and transfer them to a list. Yes, you can, of course, with the with argument, but I need less code, and the map () function is just short and simple.
-------------

num.txt
10 20

And I need to use the map argument to add all these numbers to the list

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-04
@Noy-name-boy

And just call split and get a list? Or do you need a list of type int

numbers = '10 20 30 40 4 6 7 22 2'

l = list(map(int, numbers.split()))

Either list comprehension
l = [int(number) for number in numbers.split()]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question