This question already has answers here:
How to delete an element from a list while iterating over it in Python? [duplicate]
(2 answers)
Closed 5 years ago.
I want to know how to append all the items that weren't removed into a new list.
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
def remove_values(thelist, value):
newlist = []
while value in thelist:
thelist.remove(value)
newlist.append()
bye = remove_values(challenge, max(challenge))
For example, if I remove all the 9s (the max), how do I append the rest into a new list?
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
# This will append every item to a new List where the value not is max
# You won't need 2 lists to achieve what you want, it can be done with a simple list comprehension
removed_list = [x for x in challenge if x != max(challenge)]
print(removed_list)
# will print [1, 0, 8, 5, 4, 1, 3, 2, 3, 5, 6]
Related
This question already has answers here:
Removing elements that have consecutive duplicates
(9 answers)
Remove adjacent duplicate elements from a list [duplicate]
(17 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to return a list of items from a sequence without any elements with the same value next to each other and preserving the original item order.
I've written a solution that works but my attempt to make this solution more pythonic does not work. It seems the if statement isn't working; it just returns the whole original sequence in a list.
The solution should be a function and I don't want to use imports.
Def order(iterable):
iter_list = list(iterable)
item_list = []
item_list.append(iter_list[item] for item in range(0, len(iter_list)) if iter_list[item] != iter_list[item -1])
return iter_list
I just need to know why (if iter_list[item] != iter_list[item -1) is not working
Your original code works just fine if you just take what's inside append if we somehow account for the first element. One way is:
out = [iter_list[item] for item in range(0, len(iter_list)) if (item==0 or iter_list[item] != iter_list[item -1])]
If you zip the list with the same list starting at index 1 you're quite close to your goal but you might miss the last element.
data = [0, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8]
result = [x for x, y in zip(data, data[1:]) if x != y]
print(result)
This gives you [0, 1, 2, 3, 4, 5, 6, 7], but we would expect an additional 8 in the list.
To get the last value too we can use itertools.zip_longest.
from itertools import zip_longest
data = [0, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8]
result = [x for x, y in zip_longest(data, data[1:]) if x != y]
print(result)
This results in [0, 1, 2, 3, 4, 5, 6, 7, 8].
If you don't want to duplicate the list then use islice.
from itertools import islice, zip_longest
data = [0, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8]
result = [x for x, y in zip_longest(data, islice(data, 1, None)) if x != y]
print(result)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I have a 25 elements list like
tab=[6, 5, 5, 6, 3, 3, 2, 3, 2, 5, 6, 3, 2, 4, 5, 6, 4, 2, 1, 3, 1, 1, 4, 1, 5]
And I want to create a new list tab1 which display the sum of the elements of the list two by two, if the list is odd I don't have to calculate the sum on the last element.
And it has to be done with a for loop for... any ideas?
Any help would be appreciated.
I tried on my side but without success.
i tried this :
for i in tab:
print(sum(i:len(tab))
it gives me a syntax error and i tried some of others things but didn't work...
i tried this too
for i in tab:
print(sum(i,i+1))
didn't work too
and when i do this
sum=0
for i in tab:
print(sum(tab[i]+tab[i+1]))
it gives me back : TypeError: 'int' object is not callable
You could iterate over the list with steps of 2.
orig_len = len(tab) # List length
tab1 = []
for i in range(0, orig_len - orig_len%2, 2):
tab1.append(tab[i] + tab[i+1])
print(tab1)
You have to round down the number to first smaller even number, which is that orig_len - orig_len%2 does.
Try this:
tab=[6, 5, 5, 6, 3, 3, 2, 3, 2, 5, 6, 3, 2, 4, 5, 6, 4, 2, 1, 3, 1, 1, 4, 1, 5]
print([sum(tab[i:i+2]) for i in range(0, len(tab),2)])
The output will be:
[11, 11, 6, 5, 7, 9, 6, 11, 6, 4, 2, 5, 5]
This can be achieved by:
tab = [6, 5, 5, 6, 3, 3, 2, 3, 2, 5, 6, 3, 2, 4, 5, 6, 4, 2, 1, 3, 1, 1, 4, 1, 5]
tab1 = []
initial_pos = 0
for i in range(2, len(tab), 2):
tab1.append(sum(tab[initial_pos:i]))
initial_pos = I
if i == len(tab) - 2:
tab1.append(sum(tab[i:]))
What is happening here is we are iterating through tab starting at the third element and summing the values of elements of the slice tab[0:2], then initial_pos will assume the value of 2 and I the value of 4, so in the next iteration we will be appending sum(tab[2:4])to tab1 and so forth.
The if statement makes sure this works for odd lists and even lists.
This question already has answers here:
Removing duplicates in lists
(56 answers)
One-liner to remove duplicates, keep ordering of list [duplicate]
(6 answers)
Closed 3 years ago.
I have a big list of strings appearing many times and I want a list of the same strings to appear only once.
An example with numbers would be:
a = [1, 2, 2, 3, 4, 4]
and I want to get
b = [1, 2, 3, 4]
What I tried is something like:
a = [1, 2, 2, 3, 4, 4]
[x for x in a if a.count(x) == 1]
[1, 3]
but this omits the duplicate numbers and takes only those appearing once.
You can try this:
import collections
a = [1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8]
print([item for item, count in collections.Counter(a).items()])
This question already has answers here:
Python list rotation [duplicate]
(4 answers)
Closed 8 years ago.
Suppose I have a list u = [1, 2, 3, 4, 5], and u[1:] returns [2, 3, 4, 5].
I wonder what indexing returns [2, 3, 4, 5, 1], going from the second position to the last and then the first?
You can make a general function that does this at any point in your list, just by adding two slices. This was an intentional design as to why slicing is half-open (includes left index, but excludes right index)
def rotate(l, i):
return l[i:] + l[:i]
>>> u = [1, 2, 3, 4, 5]
>>> rotate(u, 1)
[2, 3, 4, 5, 1]
>>> rotate(u, 2)
[3, 4, 5, 1, 2]
This question already has answers here:
Sorting list based on values from another list
(20 answers)
Closed 9 years ago.
I have a list of objects and I'd like to sort them based on a parallel array. So, as I operate over a list of data I construct a parallel array (where each entry in that list corresponds to an entry in the original list). Then (let's say the parallel array is filled with numbers)
list_a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
list_b = (4, 2, 5, 6, 1, 7, 3, 9, 0, 8 )
I want to sort the original list of objects based on the parallel arrays values so that the original list is sorting in ascending order by the numerical value in the other array. Is there any way to do this built into python?
sort_a_by_b(list_a, list_b)
Expected result would be:
list_a_sorted_by_b = (8, 4, 1, 6, 0, 2, 3, 5, 9, 7 )
>>> list_a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list_b = [4, 2, 5, 6, 1, 7, 3, 9, 0, 8]
>>>
>>> import operator
>>>
>>> [k for k, v in sorted(zip(list_a, list_b), key=operator.itemgetter(1))]
[8, 4, 1, 6, 0, 2, 3, 5, 9, 7]
Call the object list objects and the other list sort_keys. If you can compute sort_keys[i] from just the value of objects[i], you don't even need to build sort_keys. You should just do this:
objects.sort(key=compute_sort_key_for_object)
where compute_sort_key_for_object is the function you would use to compute sort_keys[i] from objects[i]. It's faster and more readable.
If the processing to compute sort_keys is more complex, you'll want Rohit's answer:
import operator
[k for k, v in sorted(zip(objects, sort_keys), key=operator.itemgetter(1))]