0

why do we need list() method after keys(), and map()

counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100} lst = list(counts.keys()) , if we omit list() it shows what is called view object, but that's not a list itself? and why also the list after map, I didn't get what the output of map means

4th May 2022, 1:16 PM
sum fatt
5 Antworten
0
the output of keys() is dict_keys(['chuck', 'annie', 'jan']) they call it view object, but it has the form of a list as I see it, or the question what's different between view object and a list
4th May 2022, 1:43 PM
sum fatt
+ 3
sum fatt , using the map() function returns an iterator. this is very time and memory efficient. to get the values, we use a for loop with the given iterator, so that we can do some processing with the values that are yielded one by one at the time if we wanted to get all of the values, we can use list (constructor) that will use the iterator to create a list.
4th May 2022, 2:34 PM
Lothar
Lothar - avatar
+ 2
Because the keys() method doesn't return a list. I assume same with map() but I haven't seen your code.
4th May 2022, 1:19 PM
Slick
Slick - avatar
+ 1
With a simple lookup, it says keys() method also returns a view object. You can do your own research on view objects, but i assume that they are containers such as a list or tuple with specific methods. You could think on this for weeks, or you could just learn how to use list, tuple, and dict comprehension to get your desired values in your desired container. # if counts is the same variable you supplied. lst = [key for key, value in counts.items()] #lst is now a list of the keys
4th May 2022, 2:14 PM
Slick
Slick - avatar
0
for the map : list_int = [1, 12, 15, 21, 131] list_string = map(str, list_int) print(list(list_string)), the output without the list(), is <map object at 0x7ee6cd3910> but why is it showing a memory address, and how could list() turn this address into a list
4th May 2022, 1:55 PM
sum fatt