#πŸ”’ Understanding Map

39 messages Β· Page 1 of 1 (latest)

frail heron
#

Hello. I'm learning how to use this. I have a function called split_sentences that returns a list of strings. I did

text2=[]
for line in text:
    lines=split_sentences(line)
    text2 += lines

as a result I got a list of strings. But if I do

text2=[]
text2 += list(map(split_sentences,text))

my result is a list of lists containing the strings

Why? and how to get a result like in the first example.

crisp sparrowBOT
#

@frail heron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

flat smelt
#

Or, I guess "comparable map".

fickle glen
flat smelt
#

Whoops, read the code wrong. Nvm.

frail heron
#

mmmm not clear for me... this is difficult when you begin

fickle glen
#

the first one: ```
text string : str
text list : list

for each line in the text string
split that into lines
extend the text list with the lines

the second one: ```
text string : str
text list : list
temporary list : list

create a temporary list
for each line in the text
    split that into lines
    **append** those lines to the temporary list


extend the text list with the temporary list
frail heron
#

ok maybe I had to begin with this: with open('text3.txt', 'r') as file: text = file.readlines()

#

so text is a list of strings, isn't it?

fickle glen
#

...yeah that makes more sense, if you iterated over a string you would just get its characters lol

frail heron
#

I kinda "feel" what is happening, but can't understand why the + operator extends my list in the first example and not in the second

fickle glen
#

!e ```py
numbers = [1, 2, 3, 4]
nested_numbers = [[1, 2], [3, 4]]

flat_list = []
flat_list.extend(numbers)

nested_list = []
nested_list.extend(nested_numbers)

print(flat_list)

print(nested_list)

crisp sparrowBOT
#

@fickle glen :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [1, 2, 3, 4]
002 | [[1, 2], [3, 4]]
fickle glen
#

!e now compare that to ```py
nested_numbers = [[1, 2], [3, 4]]

flat_list = []

for sub_list in nested_numbers:
flat_list.extend(sub_list)

print(flat_list)

crisp sparrowBOT
#

@fickle glen :white_check_mark: Your 3.12 eval job has completed with return code 0.

[1, 2, 3, 4]
frail heron
#

ok, so applying extend should do the trick for the second example

#

ok, It works, but why I'm not getting same result when doing text2=[] for line in text: lines=split_sentences(line) text2 += lines

#

why it adds strings and not lists?

muted mortar
#

if I have a list vals = [a, b, c] and a thing d, and i want d to be added to vals, I do vals.append(d), then I get [a, b, c, d] in vals, cool

#

now I have again a list vals = [a, b, c] and another list [e, f]

#

I want both those e and f to be appended to my vals list

#

i can naively do

for thing in [e, f]:
    vals.append(thing)
#

using the "atomic" .append I have here

#

but no, Python offers .extend -- with it, I can append many things at once, with great convenience

#

i.e., vals.extend([e, f]) will do what I want -- will go through "one by one" over the contents of [e, f], and append it to my vals

#

side note: for a list vals, vals.extend(...) and vals += ... achieve exactly the same thing

#

different ways of writing the same thing; in the end, we end up with [a, b, c, e, f] in vals, whatever type e or f might be (strings, lists, complex numbers...)

#

going back to your example, text2 is our base list and lines is the thing we would like to append stuff from to our base list

#

you assert lines is a list-of-strings, so += (or equivalently .extend) will go over lines and ask for each and every element of lines and append them one by one to the base list text2

#

In your very original message, still the same procedure applies, nothing special there

#

only that you have a list-of-lists in the list(map(split_sentences, text)) part

#

still += will ask each element of this list and append them one-by-one

#

it turns out, each element itself happens to be a list, so we append lists, not strings directly

#

quiz question (β„“oβ„“): can you write vals.append(d) as vals += ...? what can go to ... part to achieve the same thing?

#

fun question (β„“oβ„“ again): please try vals = [12, 3, 4]; vals += "really?"; print(vals), and be welcomed to duck typing! :p

#

(it turns out, for a list, right hand side of += (or the argument to .extend) can be any iterable -- it will still ask each and every element of it and append them one by one. Lists are iterables (you are able to iterate over them; they yield their individual elements [0]th, [1]th...) and so are strings (they yield "character"s) and so are many things you will encounter in your journey I sincerely hope)

frail heron
#

ah yes. So list(map()) returns say 4 lists, so I'm appending 4 lists. In the first example I'm using += in EACH of these 4 lists, that why extend "extends" the elements(strings) in each of them. Thank you sir

crisp sparrowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.