2

Hi I would like to compare two list that are different lengths and print a sorted table with items that are missing in each table. I am partially able to accomplish this and print the values that are missing in list_2. But I am unable to also print the values that are missing in list_1 from list_2, the letter 'z'. How can I perform this to get the desired output below?

list_1 = ['a', 'b', 'c', 'd', 'e', 'f']
list_2 = ['b', 'c', 'f', 'z']

table_format = '{:<10} {:<10}'
print(table_format.format('list_1', 'list_2'))
print('-' * 20)
for x in list_1:
    for y in list_2:
        if x in y:
            print(table_format.format(x, y))
            break
    else:
        print(table_format.format(x,'Missing'))

Current Output:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f        

Desired Output:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f    
Missing    z
MBasith
  • 1,407
  • 4
  • 29
  • 48
  • Have you considered using [`set`](https://docs.python.org/3/tutorial/datastructures.html#sets)? Try `set(list_2) - set(list_1)` – Patrick Haugh Jul 19 '17 at 04:13
  • That does provide the missing value but not sure how I would make that part of the table during the for loop. – MBasith Jul 19 '17 at 04:16

3 Answers3

3

One solution could be using a third list that contains all the elements of the two original lists. Then we could sort the new list and while iterating over it, we could check the existence of elements of the third list in the original ones. Actually making the third list a set would be better. Following the suggestion from Patrick Haugh, we should convert the original lists into sets too before the iteration. Thus the process will be more efficient. Why? Follow this post. Which is faster and why? Set or List?

list_1 = set(['a', 'b', 'c', 'd', 'e', 'f'])  # Or list_1 = {'a', 'b', 'c', 'd', 'e', 'f'}
list_2 = set(['b', 'c', 'f', 'z'])  # list_2 = {'b', 'c', 'f', 'z'}

list_3 = set(list_1 | list_2)
table_format = '{:<10} {:<10}'
print(table_format.format('list_1', 'list_2'))
print('-' * 20)
for elem in sorted(list_3):
    if elem in list_1:
        if elem in list_2:
            print(table_format.format(elem, elem))
        else:
            print(table_format.format(elem, 'Missing'))
    else:
        print(table_format.format('Missing', elem))

Output:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f         
Missing    z
arif
  • 524
  • 8
  • 18
  • 1
    A minor improvement, but if you save `set(list_1)` and `set(list_2)`, you can save time when you do the lookups later. – Patrick Haugh Jul 19 '17 at 04:19
  • @MBasith This sorts your data. Are you sure that's what you want? – cs95 Jul 19 '17 at 05:12
  • "...compare two list that are different lengths and print a `sorted table` with items that are missing in each table." That's why the third list has been sorted. – arif Jul 19 '17 at 05:29
1

Using an OrderedDict seems to do the job:

from collections import OrderedDict

list_1 = ['a', 'b', 'c', 'd', 'e', 'f']
list_2 = ['b', 'c', 'f', 'z']

mapping = OrderedDict()
for x in list_1:
    mapping[x] = x if x in list_2 else 'Missing'

for x in list_2:
    mapping[x] = x if x in list_1 else 'Missing'

table_format = '{:<10} {:<10}'
print(table_format.format('list_1', 'list_2'))
print('-' * 20)

for k in mapping:
    if k in list_1:
        print(table_format.format(k, mapping[k]))
    else:
        print(table_format.format(mapping[k], k))

Output:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f         
Missing    z      
cs95
  • 379,657
  • 97
  • 704
  • 746
0

You can do the same using list comprehension!

>>> print "\n".join(map(str,['list_1\tlist_2\n---------------']+[(each if each in list_1 else 'missing')+'\t'+(each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]))
list_1  list_2
---------------
a       missing
b       b
c       c
d       missing
e       missing
f       f
missing z

I've broke down the comprehension for better understanding!

>>> [each if each in list_1 else 'missing' for each in sorted(set(list_1+list_2))]
['a', 'b', 'c', 'd', 'e', 'f', 'missing']

>>> [each if each in list_2 else 'missing' for each in sorted(set(list_1+list_2))]
['missing', 'b', 'c', 'missing', 'missing', 'f', 'z']

>>> [(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]
[('a', 'missing'), ('b', 'b'), ('c', 'c'), ('d', 'missing'), ('e', 'missing'), ('f', 'f'), ('missing', 'z')]

>>> [['list_1','list_2']]+[(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]
[['list_1', 'list_2'], ('a', 'missing'), ('b', 'b'), ('c', 'c'), ('d', 'missing'), ('e', 'missing'), ('f', 'f'), ('missing', 'z')]

>>> print "\n".join(map(str,[['list_1','list_2']]+[(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]))
['list_1', 'list_2']
('a', 'missing')
('b', 'b')
('c', 'c')
('d', 'missing')
('e', 'missing')
('f', 'f')
('missing', 'z')

>>> print "\n".join(map(str,['list_1\tlist_2\n---------------']+[(each if each in list_1 else 'missing')+'\t'+(each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]))
list_1  list_2
---------------
a       missing
b       b
c       c
d       missing
e       missing
f       f
missing z
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23