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


Messages In This Thread
Exercise List Overlap - by Damian - Apr-01-2021, 03:10 PM
RE: Exercise List Overlap - by Damian - Apr-01-2021, 03:27 PM
RE: Exercise List Overlap - by bowlofred - Apr-01-2021, 04:32 PM
RE: Exercise List Overlap - by Damian - Apr-01-2021, 05:26 PM
RE: Exercise List Overlap - by ICanIBB - Apr-01-2021, 08:17 PM
RE: Exercise List Overlap - by Damian - Apr-01-2021, 08:54 PM
RE: Exercise List Overlap - by bowlofred - Apr-01-2021, 09:33 PM
RE: Exercise List Overlap - by Damian - Apr-02-2021, 07:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter text overlap Frankduc 3 3,085 Apr-01-2022, 08:02 PM
Last Post: deanhystad
  Rotated Rectangle overlap using Shapely pyNew 0 1,737 Feb-25-2021, 04:54 AM
Last Post: pyNew
  How to order the Cronjobs to avoid overlap or conflict sadhaonnisa 1 1,856 Oct-10-2020, 10:26 AM
Last Post: DeaD_EyE
  Exercise about list of lists Otbredbaron 3 3,069 May-03-2018, 09:43 PM
Last Post: micseydel
  Can OpenGL object be overlap? hsunteik 4 5,141 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