# [Tutor] problem solving with lists

alan.gauld at yahoo.co.uk alan.gauld at yahoo.co.uk
Sat Mar 19 08:24:31 EDT 2022

```   On 19 Mar 2022 10:57, marcus.luetolf at bluewin.ch wrote:

Many thanks again.

>lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'g'], ['b', 'c',
'h']]

>sub_lst = []

>for p in lst:

>    pair1 = p[0:2]

>    pair2 = list(p[0]+ p[2])

>    pair3 = p[1:]

>    if pair1 not in sub_lst or pair2 not in sub_lst or pair3 not in
sub_lst:

>        sub_lst.append(p)

>print(sub_lst)

>if pair1 not in sub_lst or pair2 not in sub_lst or pair3 not in
sub_lst:

Still yields all four sublists instead of eliminating the last 2
sublists , ['a', 'b', 'g'], ['b', 'c', 'h'],

since ‘a’, ‘b’ and ‘b’, ’c’ were already in the first 2 sublists.

I think python does not allow to check if an item is in a list with only
slices of the item to be checked ??

Python does what you ask. If you get the wrong answer then you are

But I still don't really understand what you are trying to do. Have you
looked at Dennis replies, his approach is more likely to get you what you
want....
Alan g

Von: alan.gauld at yahoo.co.uk <alan.gauld at yahoo.co.uk>
Gesendet: Freitag, 18. März 2022 19:16
An: marcus.luetolf at bluewin.ch
Cc: tutor at python.org
Betreff: Re: AW: AW: [Tutor] problem solving with lists

On 18 Mar 2022 15:12, [1]marcus.luetolf at bluewin.ch wrote:

…many thanks for your quick replay.

When I used a for loop :

>lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'g'], ['b', 'c',
'h']]

>sub_lst = []

>for p in lst:

>    pair1 = lst[p][0:2]

P is not an index its the actual sublist. So you just need

Pair1 = p[0:2]

>

> ected. But I don’t understand

what you mean with

If pair 1 not empty

Or pair2 not empty

Or pair3 not in newlist

What is the difference between pair not beeing empty or not beeing in
new_list ?

Pair not empty means pair contains anything other than the empty list.

Pair not in newlist means pair is not empty and it's contents are not I.
Newlist. Very different!

How should I define the if condition properly ?

You need, I assume,

If Pair1 not in newlist

Or pair2 not in newlist

Or pair3 not in newlist

Alan g.

References