-4

In the nested list:

x = [['0', '-', '3', '2'], ['-', '0', '-', '1', '3']]

how do I remove the hyphens?

x = x.replace("-", "")

gives me AttributeError: 'list' object has no attribute 'replace', and

print x.remove("-")

gives me ValueError: list.remove(x): x not in list.

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67

1 Answers1

1

x is a list of lists. replace() will substitute a pattern string for another within a string. What you want is to remove an item from a list. remove() will remove the first occurrence of an item. A simple approach:

for l in x:
    while ("-" in l):
        l.remove("-")

For more advanced solutions, see the following: Remove all occurrences of a value from a Python list

Community
  • 1
  • 1
David Cain
  • 16,484
  • 14
  • 65
  • 75