0

I have two problem with this code. First, it doesn't remove the '\n' at the end of the list row. So I tried printing x to see what is going on but then I ran into the second issue which is that x doesn't get printed. It seems that some line breaks and '51' gets printed, but why only those but not others?

How can this happen? I have absolutely no clue.

Edit: This is NOT a duplicate. I want to know why it doesn't remove but ALSO why it doesn't print. @Aran-Fey Read the question before you mark it as duplicate.

row = ['\n', '2017-18 ', '\n', 'GSW', '\n', '\n', '51', '\n', '32.0', '\n', '26.4', '\n', '8.4', '\n', '16.9', '\n', '49.5', '\n', '4.2', '\n', '9.8', '\n', '42.3', '\n', '5.5', '\n', '5.9', '\n', '92.1', '\n', '0.7', '\n', '4.4', '\n', '5.1', '\n', '6.1', '\n', '3.0', '\n', '1.6', '\n', '0.2', '\n', '2.2', '\n', '43.8', '\n', '5', '\n', '0', '\n', '9.5', '\n']

i = 0
for x in row:
  print(i,":", x)
  if x in ('\n', '°', '%'):
    row.remove(x)
  i+=1
print(row)

The output of this is

0 : 

1 : 

2 : 

3 : 51
4 : 

5 : 

6 : 

7 : 

8 : 

9 : 

10 : 

11 : 

12 : 

13 : 

14 : 

15 : 

16 : 

17 : 

18 : 

19 : 

20 : 

21 : 

22 : 

23 : 

24 : 

25 : 

26 : 

27 : 

['2017-18 ', 'GSW', '51', '32.0', '26.4', '8.4', '16.9', '49.5', '4.2', '9.8', '42.3', '5.5', '5.9', '92.1', '0.7', '4.4', '5.1', '6.1', '3.0', '1.6', '0.2', '2.2', '43.8', '5', '0', '9.5', '\n']

You can run this code here: https://repl.it/@leonardchoo/listnotworking

Leonard
  • 2,978
  • 6
  • 21
  • 42
  • 1
    you should never modify an object as you iterate through it. – Ma0 Jun 19 '18 at 07:42
  • @Ev.Kounis why? – Leonard Jun 19 '18 at 08:12
  • @Aran-Fey This question is not a duplicate of what you provided. Please remove the duplicate sign. Read the question before you mark it as duplicate. – Leonard Jun 19 '18 at 08:15
  • I don't see how it's not a duplicate. Removing elements from a list while iterating makes the loop skip some of the elements. It's all explained in the question I linked. – Aran-Fey Jun 19 '18 at 08:22
  • 1
    There, I found a better dupe. – Aran-Fey Jun 19 '18 at 08:26
  • @Aran-Fey Thanks, that new link makes sense. If you read my question carefully, there are two problems: 1. can't remove 2. can't print. Your new link still doesn't answer the 2nd part. That means that is only 50% of the duplicate. Hence if you mark it as duplicate, my question might not get enough attention for full answers. If it is not a full duplicate please don't mark it as one since it doesn't help me in any ways. Instead include that in the comment saying "this is the possible duplicate of..." – Leonard Jun 19 '18 at 08:33
  • *It doesn't get printed because the loop skips some elements.* It's exactly the same reason why it doesn't get deleted. – Aran-Fey Jun 19 '18 at 08:46
  • @Aran-Fey now THAT is understandable. These things may be obvious to you, but please keep in mind that it's not obvious to everyone, especially beginners. If you post it as the answer i can accept it – Leonard Jun 19 '18 at 08:52

2 Answers2

1

try this,

row = ['\n', '2017-18 ', '\n', 'GSW', '\n', '\n', '51', '\n', '32.0', '\n', '26.4', '\n', '8.4', '\n', '16.9', '\n', '49.5', '\n', '4.2', '\n', '9.8', '\n', '42.3', '\n', '5.5', '\n', '5.9', '\n', '92.1', '\n', '0.7', '\n', '4.4', '\n', '5.1', '\n', '6.1', '\n', '3.0', '\n', '1.6', '\n', '0.2', '\n', '2.2', '\n', '43.8', '\n', '5', '\n', '0', '\n', '9.5', '\n']

row = [x for x in row if x not in ['\n', '°', '%']]

for i, x in enumerate(row):
    print(i, x)

Output

0 2017-18
1 GSW
...
23 5
24 0
25 9.5
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
0

row = ['\n', '2017-18 ', '\n', 'GSW', '\n', '\n', '51', '\n', '32.0', '\n', '26.4', '\n', '8.4', '\n', '16.9', '\n', '49.5', '\n', '4.2', '\n', '9.8', '\n', '42.3', '\n', '5.5', '\n', '5.9', '\n', '92.1', '\n', '0.7', '\n', '4.4', '\n', '5.1', '\n', '6.1', '\n', '3.0', '\n', '1.6', '\n', '0.2', '\n', '2.2', '\n', '43.8', '\n', '5', '\n', '0', '\n', '9.5', '\n'] removeElement=["\n",'°','%'] for i in list(row): if i in removeElement: row.remove(i) print(row)