By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

List iteration returns only last value

P: 1
Hey guys. I am working on a pretty special program here IMO. But i keep encountering one problem. I am trying to append items from one word list to another(filtration reasons). But when the program runs, the list in the end only has the last value from the other list, whereas i know for a fact that there are more values. I am only filtering alphabets out of number.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. def cleanlist(wordlist):
  2.     for word in wordlist:
  3.         lex = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
  4.         for i in range(0,len(lex)):
  5.             word = word.replace(lex[i], "")
  6.             if len(word) > 0:
  7.                 clean_list = []
  8.                 clean_list.append(word)
  9.         print(clean_list)
  10.     f.write(str(clean_list))
Jan 2 '17 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 613
A new, empty list is created each time, so only contains that one entry. See the next line under the "if len(word) > 0:" line. Also, use "".join() to cast a list into a string.
Jan 3 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.