+ 2

Why does it work like this ?

I don't understand why each element of the list is fist splited, and then turned into a list : x = ['abc', 'defg'] y = list(map(list, x)) print(y) At first, I expected the output to be like : "[['abc'], ['defg']].

17th Jul 2022, 8:57 AM
Ali_combination
Ali_combination - avatar
3 Answers
+ 6
Ali_combination As you have passed list function in a map function so map function will convert each item to a list so 'abc' will be converted in ['a', 'b', 'c'] Same for 'defg' so result is [['a', 'b', 'c'], ['d', 'e', 'f', 'g']]
17th Jul 2022, 9:37 AM
AĶ¢J
AĶ¢J - avatar
+ 4
List creates a mutable sequence from a string iterator, and the string as a whole can become a list element when using the append method or representing the string via a list comprehension: print([ [s] for s in ["do", "smth"] ])
17th Jul 2022, 10:30 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 2
Vitaly Sokol I see thanks.
17th Jul 2022, 11:04 AM
Ali_combination
Ali_combination - avatar