# Python: Remove duplicates from a list of integers, preserving order

## Python Programming Puzzles: Exercise-54 with Solution

Write a Python program to remove duplicates from a list of integers, preserving order.

Input: [1, 3, 4, 10, 4, 1, 43] Output: [1, 3, 4, 10, 43] Input: [10, 11, 13, 23, 11, 25, 23, 76, 99] Output: [10, 11, 13, 23, 25, 76, 99]

**Visual Presentation:**

**Sample Solution-1:**

**Python Code:**

```
# Define a function named 'test' that takes a list of numbers as input and removes duplicates while preserving the original order
def test(nums):
# Use a dictionary to convert the list to a set, effectively removing duplicates, and then convert it back to a list
return list(dict.fromkeys(nums))
# Create a list of numbers named 'nums'
nums = [1, 3, 4, 10, 4, 1, 43]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create another list of numbers named 'nums'
nums = [10, 11, 13, 23, 11, 25, 23, 76, 99]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))
```

Sample Output:

Original list of numbers: [1, 3, 4, 10, 4, 1, 43] Remove duplicates from the said list of integers, preserving order: [1, 3, 4, 10, 43] Original list of numbers: [10, 11, 13, 23, 11, 25, 23, 76, 99] Remove duplicates from the said list of integers, preserving order: [10, 11, 13, 23, 25, 76, 99]

**Flowchart:**

**Sample Solution-2:**

**Python Code:**

```
# Define a function named 'test' that takes a list of numbers as input and removes duplicates while preserving the original order
def test(nums):
# Initialize an empty list named 'result' to store unique elements
result = []
# Iterate through each element 'x' in the input list 'nums'
for x in nums:
# Check if 'x' is not already present in the 'result' list
if x not in result:
# If not present, append 'x' to the 'result' list
result.append(x)
# Return the final list with duplicates removed
return result
# Create a list of numbers named 'nums'
nums = [1, 3, 4, 10, 4, 1, 43]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Create another list of numbers named 'nums'
nums = [10, 11, 13, 23, 11, 25, 23, 76, 99]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))
```

Sample Output:

Original list of numbers: [1, 3, 4, 10, 4, 1, 43] Remove duplicates from the said list of integers, preserving order: [1, 3, 4, 10, 43] Original list of numbers: [10, 11, 13, 23, 11, 25, 23, 76, 99] Remove duplicates from the said list of integers, preserving order: [10, 11, 13, 23, 25, 76, 99]

**Flowchart:**

**Python Code Editor :**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:** Find the product of the units digits in the numbers.

**Next:** Find the numbers that are greater than 10 and have odd first and last digits.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.**

https://www.w3resource.com/python-exercises/puzzles/python-programming-puzzles-54.php

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics