comparing two lists
MRAB
python at mrabarnett.plus.com
Wed Feb 24 20:30:06 EST 2021
On 2021-02-25 00:42, Davor Levicki wrote:
> i have two lists
>
> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>
> and when I loop through the list
>
>
> list_difference = []
> for item in list1:
>
> if item not in list2:
> list_difference.append(item)
>
>
> and I managed to get the difference, but I need time as well
> because it is a separate item and 'uvz' does not mean to me anything in the list with a few thousand entries.
> I tried to convert it to the dictionary, but it overwrites with last key:value {'01:15' : 'def'}
>
If the items belong in pairs, try making the pairs first:
>>> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
>>> list(zip(list1[0 : : 2], list1[1 : : 2]))
[('01:15', 'abc'), ('01:15', 'def'), ('01:45', 'ghi')]
and then work from there.
More information about the Python-list
mailing list