How to remove an element from LinkedList in stable Rust? - rust

I have a linked list of values and I want to remove elements from there either by value or by index. I tried to use the remove function, but the compiler says that this function is not stable. Is it possible to remove elements from an arbitrary location in a linked list using only stable Rust?

You can split the array using split_off then join them back together without the element. See this comment on github
// Split the list in 2
let split_list = original_list.split_off(index_to_remove);
// Remove the first element of the second half
split_list.pop_front();
// Join the 2 halves back together, except for the middle element
original_list.append(split_list);

Related

iterating over list in list; python

I have a simple problem. I'm new to python and programming so I think i miss something.
The variable "account_info" is assigned earlier and is a list of lists with 4 elements each. The variable current is a user input value, which (should) appear as the first element of the lists in the list account_info.
I want to iterate over the lists in the list and compare if the first element is equal to "current".
This is the code:
for i in account_info:
if current == account_info[i][0]:
email = account_info[i][1]
additional = account_info[i][2]
pw = account_info[i][3]
print(email)
I get an error in pycharm, when running that code. It seems that I can't iterate over the lists like that, can please someone explain and show a different solution?
Thank you
As #ForceBru commented, your issue is due to how for loops in Python work. The value you get from the loop is not an index into the iterable object you're looping on, rather, it's a value from the iterable. That makes your indexing with it later almost certainly wrong (though in certain contexts it might make sense, if you have a list that contains indexes into itself).
In your case, you probably want to do something more like this:
for account in accounts_info:
if current == account[0]: # note, only the inner indexing is needed
email = account[1]
additional = account[2]
pw = account[3]
Since you're expecting the inner lists to contain four values, you could even unpack the account values that you get from iterating directly into the inner variables. Though this would happen unconditionally, so it might not do what you want. Here's what that would look like, with the print call you were doing after the loop instead moved inside the conditional (so you only print the one email address that corresponds to the value in current):
for account_id, email, additional, pw in account_info: # unpack unconditionally
if account_id == current: # use the convenient name here
print(email) # print only in the conditional
In the rare case where you really do need to iterate over indexes, you can use the range type, which behaves like a sequence of integers (starting at zero by default). So you could replace your loop with this version and the body would work as you had intended (though this is less idiomatic Python than the previous versions).
for i in range(len(accounts_info)):
If you need both the index and the current value, you can use the enumerate function, which yields 2-tuples of index and value as you iterate over it. This is often handy when you need to reassign values in a list some times:
for i, account in enumerate(accounts_info):
if account[0] == current:
accounts_info[i] = new_value # replace the whole account entry

list index out of range while writing a program to delete duplicate elements

I am trying to delete duplicate elements, but it shows list index out of range error at l=n[i]
n=list(input().split())
for i in range(len(n)):
l=n[i]
for j in range(len(n)):
if(j!=i):
if(l==n[j]):
n.pop(j)
print(n)
It shows error because you pop elements from inside, which has changed the size of list while iterating. But the range function is still having the original length and thus gives an index which is no longer existing.
You should never change the length of a list (or keys of a dictionary) while iterating over it.
In detail, let's take your list n=[1,1,2,3] (say). i will run in range(3)(i.e. take values 0,1,2,3)
Now, l=n[0], i.e. l=1. Then at j=1, n[j]=1, the condition l==n[j] turns true and you do n.pop(1).
So now, your list n=[1,2,3] BUT your outermost loop is still in range(4) and thus will give error when you do l=n[3] since n[3] doesn't exist.
To avoid this, you've following options:
Push the non-duplicate elements into a new list. (Or alternatively, make a copy of the original list and pop from there).
Use a set on the list, if you are allowed to use it.
Also, side note: list(input().split()) is redundant call to the list constructor. .split() method returns a list by default.
So just n=input().split() is enough.

Program in to generate chained list in python

I am developing a program in python and as part of it I have to link all the lists that have an element in common in a certain position, that is, there is an input element and an output element and I want to gather all those that follow the chain. For example, we have as input a list :
list_array = [[n_element, l_input, l_ouput], .....]
A concrete example would be
list_array = [[1,a,b],[2,c,d],[3,e,f],[4,b,e],[5,d,f],[6,a,e],[7,b,c]
The result of the program should be a list where the elements are linked by input and output.
res_array = [[1,4,3],[1,7,2,5],[6,3]]
The result of the program should be a list where the elements are linked by input and output. If there is one element included in another, the element with greater length prevails. My first thought was to use a tree structure, a search in depth or length. I need ideas.
You could use a graph representation of this problem:
For each element [n_element, l_input, l_output], you add vertices l_input and l_output to your graph (if not already present) and add an edge labelled n_element (from l_output to l_input).
Then, you look for paths through that graph. The resulting list is then given by the concatenation of edge labels.

In nim language, how to search for the index of the first appearance of an object

Newbie in nim here.
While experimenting in nim, I didn't find a proc to be able to find the first occurrence of an item in a sequence.
What I practically want to do is, given a seq, to remove the first instance of a known string, and return the same sequence, with the item removed. So I was thinking, to break it in a two-step work: first find the index and then remove the item at the specific index.
Of course I can write my own helper function for this, but I am surprised that I couldn't find in the system or sequtils modules any similar solution.
That would just be:
var s = #["a", "b", "c"]
s.del(s.find("b"))
The del function modifies the sequence in place. You would start with a copy of the sequence if you want a new value.

How to loop over a list incrementally

listname = ['bartian', 'lenana', 'kilimanjaro', 'uhuru', 'elgon', 'everest']
while True:
if listname[:-1] == everest:
print(listname[:-1]+=)
I need a way to loop over this list and print the items in the list. I dont know whether this is pythonic since am having trouble printing the items. I want it to print from the last to first, middle item to lst or to first.
I think the easiest way to solve your task is by using a for loop on a slice of your original list.
for name in listname[::-1]:
print(name)
This prints the names in reverse order. The [::-1] slice says to go from one end to the other, with a step size of -1. For this specific case, you could also use reversed, but if you want to do other kinds of slicing it might make sense to use a slice here too, for symmetry.
Here are slices for the other forms you wanted:
for name in listname[len(listname)//2:]: # iterate on a slice from middle to end
print(name)
for name in listname[len(listname)//2::-1]: # slice from the middle back to the start
print(name)
Your code also has an if statement in it, but you don't describe what you want for it to do. If you're only printing names that are "everest" there's not much point to the loop!

Resources