Day 050 - Project 50 : Idea Storage System

Thanks! I’ve needed that for some code pretty recently and I couldn’t remember how to do it.

2 Likes

Thank you. Can you do a breakdown for what each code means?

Can you explain please what this code means?

Basically, it loops through every line in the file, strips each line, and creates a list of the stripped lines.

2 Likes

Thank you so much! Is this a breakdown of the function of .split()?

I didn’t realize you can have the “\n” inside of .split().

1 Like

Also I tried your code but it wrote “IndexError: list assignment index out of range”. Did I miss something?

No, I probably just wrote it incorrectly, because I was trying to write it off memory, that’s my bad.

Just fixed and added comments to each line of my code snippet, that should work now.

You’re welcome and yes, you can tell .split() where to split, and how many times you want it to split.

text = "abc.def.ghi"

edit = text.split(".")
edit2 = text.split(".",1)#only splits once, on the first . 

print(edit) # prints ['abc', 'def', 'ghi']
print(edit2) # prints ['abc', 'def.ghi']
3 Likes

how about storage = [line for line in file]

pretty sure that is valid…

idk if storage = list(file) or storage = [*file] works, prob not.

2 Likes

Those don’t strip the line, though?

2 Likes

add a map(strip, storage)?

storage = map(lambda x: x.strip(), list(file))

Is probably valid, but a little less readable imo, although maybe I just really like the [x for x in iterable] syntax.

2 Likes

I’m a bit confused about edit2. By split 1, is it assuming abc as [0] or “.”, and def.ghi as [1] as in the list?

2 Likes

We’re telling it to split wherever there is a period. But the 1 tells it to only do it one time, so it splits on the first period but not the rest.

So abc becomes edit2[0] and def.ghi becomes edit2[1]

3 Likes