Apr-01-2021, 03:10 PM
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)
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)