I'm trying to remove all the numbers from a list, but when I run the code, I get a strange result:
nums = [1, 2, 3, 4, 5]
words = ["I", "sing", "of", "arms", "and", "a", "man"]
both = nums + words
for each in both:
if each in range(1000):
both.remove(each)
print(both)
Output:
[2, 4, 'I', 'sing', 'of', 'arms', 'and', 'a', 'man']
Process finished with exit code 0
What are the 2 and 4 still there?
You are modifying a list that you are looping through. Here's an idea of what is happening:
- Start at position 0 with value 1
- Remove element at position 0, all following elements are shifted back by one
- Continue at position 1 with value 3 (you skipped over 2)
- Remove element at position 1, all following elements...
- Continue at position 2 with value 5 (you skipped over 4)
- Remove element at position 2, ...
- All following elements are preserved because they do not meet the if test.
One solution is to create an empty list and append to it all elements that you would not remove from the original list:
both = nums + words
new_list = []
for each in both:
if each not in range(1000):
new_list.append(each)
I haven't tested this but it should do the trick.
I would approach it this way:
Take away ambiguity: 'number' is integer.
Make a plan. In spoken language: "give me items in list which are not integers".
Find the pieces:
- iterate over elements: use for-loop
- test whether element is integer: use built-in functions
isinstance()
- wrap it into list comprehension
>>> lst = [1, 'one', 2, 'two']
>>> no_ints = [item for item in lst if not isinstance(item, int)]
>>> no_ints
['one', 'two']
EDIT: if 'number' is both integers and floats then element testing should be
isinstance(item, (int, float))
The solution turned out to be fairly simple. From the official documentation:
Quote:If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy. The slice notation makes this especially convenient.
This fixed the problem:
list1 = ["I", "sing", "of", "arms", "and", "a", "man"]
list2 = [1, 2, 3, 4, 5]
list3 = list1 + list2
for x in list3[:]:
if x in range(10):
list3.remove(x)
print(list3)
Output:
['I', 'sing', 'of', 'arms', 'and', 'a', 'man']
Process finished with exit code 0
There are too much unnecessary 'actions' in code.
You create new list, then create copy of that new list and then remove items from new list to get list without integers. New list and it's copy are throw-away lists.
More efficient could be to iterate over all items in both lists and 'take out' only these which are not integers.
list_1 = ["I", "sing", "of", "arms", "and", "a", "man"]
list_2 = [1, 2, 3, 4, 5]
list_3 = []
for item in [*list_1, *list_2]:
if not isinstance(item, int):
list_3.append(item)
One can do it with list comprehension as well:
list_3 =[item for item in [*list_1, *list_2] if not isinstance(item, int)]
In your code You check only for integers 0...9 which means that integers 10 and larger / less than 0 will be kept.
(Aug-26-2019, 07:52 AM)perfringo Wrote: [ -> ]There are too much unnecessary 'actions' in code.
You create new list, then create copy of that new list and then remove items from new list to get list without integers. New list and it's copy are throw-away lists.
More efficient could be to iterate over all items in both lists and 'take out' only these which are not integers.
list_1 = ["I", "sing", "of", "arms", "and", "a", "man"]
list_2 = [1, 2, 3, 4, 5]
list_3 = []
for item in [*list_1, *list_2]:
if not isinstance(item, int):
list_3.append(item)
One can do it with list comprehension as well:
list_3 =[item for item in [*list_1, *list_2] if not isinstance(item, int)]
In your code You check only for integers 0...9 which means that integers 10 and larger / less than 0 will be kept.
In general, when do you use
isinstance()
and when do you use
==
?
(Aug-26-2019, 11:12 PM)Exsul Wrote: [ -> ]In general, when do you use isinstance()
and when do you use ==
?
They accomplish quite a different tasks:
-
==
compares values of two objects for equality
Documentation >> The Python Language Reference >> 6. Expressions >> 6.10.1 Value Comparisons
-
isinstance([i]object, classinfo[/i])
checks whether object is instance of class (or subclass) (instance of class is fancy way to say what type the object is - int, float, list etc).
You use former if you need to check equality:
>>> a = 42
>>> a == 42
True
>>> a == 43
False
You use latter for identity. As in this task - you want to eliminate all integers regardless of the value, so you check whether object is integer and act accordingly.
>>> a = 42
>>> isinstance(a, int)
True
>>> isinstance(a, float)
False
>>> isinstance(a, (int, float))
True