Pop method removes random element instead of top element in Python? - python

In Python when we use pop method to remove an element from set why does it remove any random element? Instead of removing top element as per the stack's nature.

Because a set is an un-ordered collection of items, by definition. There it no "first" or "last" element. So, it removes a random one. If you want order, use a list instead.

Related

Duplicate element being added through for loop

NOTE: I do not want to use del
I am trying to understand algorithms better which is why I want to avoid the built-in del statement.
I am populating a list of 10 randomly generated numbers. I then am trying to remove an item from the list by index, using a for loop:
if remove_index < lst_size:
for value in range(remove_index, lst_size-1):
lst[value] = lst[value+1]
lst_size -= 1
Everything works fine, except that the loop is adding the last item twice. Meaning, if the 8th item has the value 4, it will add a 9th item also valued 4. I am not sure why it is doing this. I still am able to move the value at the selected index (while moving everything up), but it adds on the duplicate.
Nothing is being added to your list. It starts out with lst_size elements, and, since you don't delete any, it retains the same number by the time you're done.
If, once you've copied all the items from remove_index onwards to the previous index in the list, you want to remove the last item, then you can do so either using del or lst.pop().
At the risk of sounding flippant, this is a general rule: if you want to do something, you have to do it. Saying "I don't want to use del" won't alter that fact.
Merely decrementing lst_size will have no effect on the list - while you may be using it to store the size of your list, they are not connected, and changing one has no effect on the other.

Find an item by its position in a grid tkinter

In tkinter, is there a way for me to reference a widget within a grid by its row and column, in the same way that you would be able to reference an item within a list (or list of lists) by knowing its position in the list?
You can call the .grid_slaves(row, column) method on the parent widget; this will return a list (possibly empty) of the widgets in that cell.
You could also iterate over all of the child widgets (.grid_slaves() with no parameters, or .winfo_children()) and call .grid_info() on each one. This returns a dictionary with 'row' and 'column' keys, along with various other grid parameters.
Actually, I realised that I could solve my own problem in a much simpler way, by literally making a list of lists, with each sub-list containing all of the widgets for a single row, and therefore I can refer to each item through it's row and column.

How to make nested lists with automatic numbering in Sphinx?

I am using Sphinx to write a documentation for a Python program.
I would like to create an ordered list, with a nested list, and with automatic numbering.
I would like to obtain something like this:
Here it follows a numbered list
This is the first item of the list
This is the second one
The second item has a nested list with two items
this is the last item of the nested list
The parent list continues with its third item
Everything is ok if I explicitly use numbers for the numbered list. The issue raises when I want to use automatic numbering, because I want to remain flexible. The list is long and could change in the future, and I do not want to change all of the numbers when I introduce a new item.
I have tried using the following code:
Here it follows a numbered list
#. This is the first item of the list
#. This is the second one
* The second item has a nested list with two items
* this is the last item of the nested list
#. The parent list continues with its third item
And the result I get is the following:
Here it follows a numbered list
This is the first item of the list
This is the second one
The second item has a nested list with two items
this is the last item of the nested list
.1. The parent list continues with its third item
[I have to add some character, here a dot, for the 3rd item or the markdown system in stackoverflow shows a 3!]
As you can see the numbering after the nested list restarted from the beginning.
You have to properly indent the two items of the nested bullet list to match the text of the parent list, so just add a space like this:
#. This is the first item of the list
#. This is the second one
* The second item has a nested list with two items
* this is the last item of the nested list
#. The parent list continues with its third item
Full explanation here

How can I delete an element from a multidimensional array in Python?

I'm trying to make a tree structure on python using a multidimensional array on python. I'm using this line of code to create my list:
matrix = [[-1 for x in range(5)] for x in range(3)]
If I print the list it will return this:
[[-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1]]
This next paragraph is a small explanation of what I'm doing just in case anyone is interested or if it helps you give a more complete solution for my problem. The actual question is in the next paragraph.
The first position within the array is the key or name of the element, the second position is who is the parent node of the element, the third position tells you if the node has any children and the last two positions tell you who the predecessor and successor of the element are, if there are any.
The question starts here:
Anyway I'm also required to create a garbage collector that collects any node and its children if I were to choose to delete the node. However I don't know how to delete the entire element. I found another thread that suggested using something called pop to delete things from the multidimensional array. But from what I gathered after reading the answers the pop method only deletes an element within the list meaning it would only delete a -1 instead of deleting everything. So is there any method of deleting one lists within the list in its entirety.
pop deletes an element in a list even if that element is a list so to remove the first list in matrix you can use
matrix.pop(0)

why django multivaluedict get returns last element

I have used Django MultiValueDict many times and all the times either I use the entire list stored with a key or I want to use the first value from the list. A common use case is use it as form initial data.
My problem is that by default Django MultiValueDict's get method returns last element.
Do I have to override getitem of MultiValueDict or is there any better alternative ?
You can use:
mv_dict.getlist()[index]
Where index is the index of the element you want in the list. For example 0 to get the first element.
Check https://github.com/django/django/blob/master/django/utils/datastructures.py#L285
But certainly if for some reason you always want to return the first element of the list, subclassing MultiValueDict sounds reasonable. It depends on your use case.

Categories