Python Forum

Full Version: List comparison in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
@Vysero : ok let me elaborate . Taking this example :
if " vlan 158 " of list a is present anywhere in list b then "ignore".same for list b (if " vlan 158 " of list b is present anywhere in list a then "ignore").
"vlan 159 " of list b is not available anywhere in list a , so it should return remaining element along with first element.So it should say list a is below missing config . 

['vlan 159', '  name SALES', '  mode vpc']


In short if first element ( vlan id : vlan 158 ,vlan 159 etc ) of a list is present in other list then ignore and vice versa , if not then return with remaining element .
Hrm, so if I have this right then your looking for something like this:

a = [['vlan 158', '  name MARKET', '  mode vpc']]
b = [['vlan 158', '  name MARKETING', '  mode vpc'], ['vlan 159', '  name SALES', '  mode vpc']]


def check_contents(list_one, list_two):
  shorter, longer = sorted([list_one, list_two], key=len)
  for x in longer:
    if not x:
      continue
    for y in shorter:
      if not y:
        continue
      if x[0] != y[0]:
        print(x)

check_contents(a, b)
Output:

['vlan 159', '  name SALES', '  mode vpc']
Thanks
(Aug-09-2018, 02:42 PM)Nirmal Wrote: [ -> ]Thanks

Sure no problem!
Pages: 1 2