G+_Nevrin O Posted April 27, 2014 Share Posted April 27, 2014 Python program that reads a list of names from a file. if no file exists it creates one with a few names already in it. then it allows for user to add/remove names and finishes with saving back to file. https://github.com/NevrinO/Coding/blob/master/Python/Lists/files%20%27n%20names.py Link to comment Share on other sites More sharing options...
G+_L I Posted April 27, 2014 Share Posted April 27, 2014 A question and an observation: 1. I see you wrote a function to do the check to see if the name I want to delete is in the list. I tried replacing with "if element in namesList" but it didn't work. I thought I'd used that successfully before to check if an element is in a list. Any ideas why it doesn't work here? 2. When deleting by index, the pop and message is contained in the same line of code which, when you type in an invalid number, leads to a message like "successfully removed there is no name located..." Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted April 27, 2014 Author Share Posted April 27, 2014 Thanks for finding that bug with the index (it made sense at 4:00am) :P as for the contains function idk why it isn't working for you i actually changed it to your code and it works fine. It actually saved me an entire function worth of code doing that (thank you) I guess I was just treating it like an array and not a list and assumed I had to look at each element. Link to comment Share on other sites More sharing options...
G+_L I Posted April 27, 2014 Share Posted April 27, 2014 Cool. I'll try it again! Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted April 29, 2014 Share Posted April 29, 2014 Cool little program. Removing empty elements from a list is another thing you can do with a single line using a list comprehension: namesList = [s for s in namesList if s != ""] Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted April 29, 2014 Author Share Posted April 29, 2014 Thanks for that Darryl, significantly more efficient then what I had. Now that line should be O(n). Updated my code and added lot more comments for anyone interested. Link to comment Share on other sites More sharing options...
Recommended Posts