Why does unified_diff method from the difflib library in Python leave out some characters?

6.3k Views Asked by At

I am trying to check for differences between lines. This is my code:

from difflib import unified_diff

s1 = ['a', 'b', 'c', 'd', 'e', 'f']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n']

for line in unified_diff(s1, s2):
    print line

It prints:

---
+++ 

@@ -4,3 +4,9 @@

 d
 e
 f
+g
+i
+k
+l
+m
+n

What happened to 'a', 'b', and 'c'? Thanks!

1

There are 1 best solutions below

0
On

If you take a look at unified_diff code you will find description about a parameter called n:

Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three.

In your case, n basically indicates numbers of characters. If you assign a value to n, then you will get the correct output. This code:

from difflib import unified_diff

s1 = ['a', 'b', 'c', 'd', 'e', 'f']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n']

for line in unified_diff(s1, s2,n=6):
    print line

Will generate:

--- 

+++ 

@@ -1,6 +1,12 @@

 a
 b
 c
 d
 e
 f
+g
+i
+k
+l
+m
+n