Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exercise List Overlap
#1
Hi all,

I've got an exercise and I'm trying to solve it.

I'm a Python beginner.

EXERCISE
Take two lists (you can use the ones in the example below) and write a program that prints a list that contains only the elements of the two lists that are equal and in the same position without duplication.
e.g.,
a = [5, 3, 5, 44, 4, 55, 4]
b = [5, 4, 55, 4, 4, 55, 4, 55, 9, 10, 11]


So, this is what I've done:

a = [5, 3, 5, 44, 4, 55, 4]
b = [5, 4, 55, 4, 4, 55, 4, 55, 9, 10, 11]

list_of_tuples = list(zip(a, b))
result = []
for tuple in list_of_tuples:
if tuple[0] == tuple[1]:
result.append(tuple[0])

print(result)
# [5, 4, 55, 4]

Then I've to keep the list order and skip duplicates.

What do you suggest me to do?
Do you think there's a better way of solving the problem from the beginning? (from the point of view of a beginner of course)
Reply
#2
If I use a set:

print(list(set(result)))

I get [4, 5, 55]

I don't understand why Python re-arrange the list when I create a set...


The result should be [5, 4, 55] because the order matters
Reply
#3
sets don't keep track of the order that things were added. If you care about the order, you need something other than a set. They're great for removing duplicates, which is not useful here.

This exercise looks like a good use for zip(). It takes in two streams and combines them, keeping the order. That's exactly what is needed here.
Reply
#4
Thank you for your swift reply
Reply
#5
Dance Dance Dance
Reply
#6
So, to finish off I can write something like this:

without_duplicates = []
for element in result:
   if element not in without_duplicates:
       without_duplicates.append(element)

print(without_duplicates)
Is there an easier way to solve it?

Thank you all
Reply
#7
Bah, I missed the "without duplicates" on the original problem. Sorry for the confusion.

You can use the set to remove duplicates, but then you'd have to refer back to the original list for the order.

But with Python 3.6 (I think), dicts not only remove duplicates but also keep order. So you can shove them into a dict and then pull the keys out.

>>> l
[5, 4, 55, 4]
>>> list(dict.fromkeys(l))
[5, 4, 55]
Reply
#8
Wowww, this is cool!

This is a nice suggestion :) Thank you all
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter text overlap Frankduc 3 2,996 Apr-01-2022, 08:02 PM
Last Post: deanhystad
  Rotated Rectangle overlap using Shapely pyNew 0 1,711 Feb-25-2021, 04:54 AM
Last Post: pyNew
  How to order the Cronjobs to avoid overlap or conflict sadhaonnisa 1 1,836 Oct-10-2020, 10:26 AM
Last Post: DeaD_EyE
  Exercise about list of lists Otbredbaron 3 3,030 May-03-2018, 09:43 PM
Last Post: micseydel
  Can OpenGL object be overlap? hsunteik 4 5,103 Jan-19-2017, 02:43 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020