2

should look something like this:

if items in both lists (equal/ larger/smaller):
   do something
else:
   if items in both lists (equal/ larger/smaller):
       do something
   else:
       do something
A.Woods
  • 21
  • 3
  • Your earlier example is basically a loop where you set n to increase, so why not use the for loop? In any case, if you really want to use the while loop then you need to identify clearly when your while loop ends instead of the for loop which ends after iterating through every element of the list. – BernardL Oct 17 '18 at 03:36

1 Answers1

1

Well, it's unclear but seems like this is what you want:

list3 = [i for i in list2 if i not in list1]

Now:

print(list3)

Is:

[4]

If one element list, you care about, do:

print(next(iter(list3),list3))

Then output:

4

And if list3 is a list of more than 1 element for example:

[1,2]

This will still output:

[1,2]

Or ^ operator for sets (second part for slicing out first half of the list):

list3 = list(set(list2)^set(list1))
list3=list3[len(list3)//2:]

Now:

print(list3)

Is:

[4]

Then also can use next stuff for list3

Or can do symmetric_difference:

list3 = set.symmetric_difference(set(list1),set(list2))
list3=list(list3)[len(list3)//2:]

Now:

print(list3)

Is:

[4]

Can do same stuff for next

And as you want a while loop:

l=[i for i in list2 if i not in list1]
it=iter(l)
while l!=list3:
   list3.append(next(it))

A dummy way of doing this tho ...

And now:

print(list3)

Is:

[4]

And can do same next stuff

Or even better:

num = 0
while num < len(list2):
    if list2[num] != list1[num]:
        list3.append(list2[num])
    num += 1

And now:

print(list3)

Is:

[4]

And can do same next stuff

U13-Forward
  • 69,221
  • 14
  • 89
  • 114