Skip to content

Commit 3f1535f

Browse files
committed
code corrected, thanks to my friend for letting me know
1 parent daaca6e commit 3f1535f

File tree

2 files changed

+31
-48
lines changed

2 files changed

+31
-48
lines changed
Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
### Code Snippet: Remove Duplicates from List
2-
3-
#### Inputs
4-
The code snippet prompts the user to enter a list of elements separated by space.
5-
6-
#### Flow
7-
1. The code snippet takes input from the user, expecting a string of elements separated by spaces.
8-
2. It converts the input string into a list of strings.
9-
3. It converts the list of strings into a list of integers.
10-
4. It creates a set from the list to remove duplicate elements.
11-
5. It converts the set back to a list to retain the distinct elements.
12-
6. It prints the list containing only the distinct elements.
13-
14-
#### Outputs
15-
The code snippet outputs a list with duplicate elements removed.
1+
## Summary
2+
This code snippet removes all occurrences of a given element from a Python list.
163

4+
## Example Usage
175
```python
18-
# getting the input from the user
19-
data = input("Enter the list elements separated by space: ")
20-
21-
# converting the string to list
22-
list_data = data.split()
23-
24-
# converting the list to integers
25-
list_data = [int(i) for i in list_data]
26-
27-
# creating a set to remove the duplicates
28-
unique_set = set(list_data)
29-
30-
# converting the set back to list
31-
distinct_list = list(unique_set)
32-
33-
# printing the list
34-
print(distinct_list)
6+
Enter list of elements separated by comma: 1,2,3,4,5
7+
Enter element to be removed: 3
8+
[1, 2, 4, 5]
9+
```
10+
11+
## Code Analysis
12+
### Inputs
13+
- `L`: A list of elements entered by the user, separated by commas.
14+
- `x`: The element to be removed from the list.
15+
___
16+
### Flow
17+
1. The user is prompted to enter a list of elements and the element to be removed.
18+
2. The input list is converted from a string to a list of integers.
19+
3. List comprehension is used to create a new list (`result`) that contains all elements from the input list (`L`) except for the element to be removed (`x`).
20+
4. The `result` list is printed.
21+
___
22+
### Outputs
23+
- `result`: A new list that contains all elements from the input list except for the element to be removed.
24+
___
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1+
# removing all occurrences of an element from a Python list
12

2-
# getting the input from the user
3-
data = input("Enter the list elements separated by space: ")
3+
# taking input from the user
4+
L = input("Enter list of elements separated by comma: ").split(",")
5+
x = int(input("Enter element to be removed: "))
46

5-
# converting the string to list
6-
list_data = data.split()
7-
8-
# converting the list to integers
9-
list_data = [int(i) for i in list_data]
10-
11-
# creating a set to remove the duplicates
12-
unique_set = set(list_data)
13-
14-
# converting the set back to list
15-
distinct_list = list(unique_set)
16-
17-
# printing the list
18-
print(distinct_list)
7+
# converting string to int
8+
L = [int(i) for i in L]
199

10+
# using list comprehension to remove all occurrences of x from L
11+
result = [i for i in L if i != x]
2012

13+
print(result)
2114

0 commit comments

Comments
 (0)