I am trying to compare two lists in python and produce two arrays that contain matching rows and non-matching rows, but the program prints the data in an ugly format. How can I clean I go about cleaning it up?
-
Possible duplicate of [How to print a list in Python "nicely"](https://stackoverflow.com/questions/1523660/how-to-print-a-list-in-python-nicely) – Jul 02 '18 at 18:55
-
Looks like the OP wants it sorted as well – rahlf23 Jul 02 '18 at 18:56
2 Answers
If you want to read the file without the \n character, you might consider doing the following
lines = list1.readlines()
lines2 = list2.readlines()
would read your file without the "\n" characters
Alternatively, for each line, you can do .strip("\n")

- 1,043
- 2
- 15
- 32
The "ugly format" might be because you are using print(match)
(which is actually translated by Python to print ( repr(match) )
, printing something that is more useful for debugging or as input back to Python - but not 'nice'.
If you want it printed 'nicely', you'd have to decide what format that would be and write the code for it. In the simplest case, you might do:
for i in match:
print(i)
(note your original list contains \n
characters, that's what enumerating an open text file does. They will get printed, as well (together with the `\n' added by print() itself). I don't know if you want them removed or not. See the other answer for possible ways of getting rid of them.

- 5,189
- 3
- 12
- 27