#๐Ÿ”’ deleting long label

65 messages ยท Page 1 of 1 (latest)

boreal wharf
#

i have an array which gathers user input which is like 60 letters long. how to delete it in array?

remote hingeBOT
#

@boreal wharf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grand marten
boreal wharf
#

ill put comment to these ideas

grand marten
#

!code

remote hingeBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

grand marten
#

you got them up there

boreal wharf
#

from kivymd.app import MDApp
from kivy.core.window import Window
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
from kivy.clock import Clock

Window.size = (360, 600)

class MyApp(MDApp):
    def build(self):
        self.data=[]
        return


    def GoToSecondScreen(self):
        self.root.current = 'second'
    def GoToFirstScreen(self):
        self.root.current = 'first'    

        
    # for My DropDown



    def open_menu(self, caller):
        menu_items = [
            {"text": "About", "on_release": lambda: self.menu_callback("About Us")}
        ]
        self.menu = MDDropdownMenu(
            caller=caller,
            items=menu_items,
            width_mult=4,
        )
        self.menu.open()

    def menu_callback(self, text_item):
        print(f"You selected: {text_item}")
        self.menu.dismiss()

    
    # for my function e.g add, update, delete
    def add_card(self):
        text = self.root.ids.TextField.text.strip()
        if text == '':
            return
        self.data.append(text)

        card = MDCard(
        size_hint=(0.45, None),  # width relative, height fixed
        height=150,
        radius=15,
        md_bg_color=(0, 1, 0, 1)
        )
        card.add_widget(MDLabel(text=text, halign="center"))

        #my idea is if i click this label i want to have an ability to delete it.
        #it is storing 50letters more or so.

        self.root.ids.listview.add_widget(card)

        self.root.ids.TextField.text = ""


MyApp().run()



#

this is a todo list app

#

@grand marten

#

got an idea?

slate jasper
#

So you want to delete a particular index of array when a certain event is triggered?

boreal wharf
#

that's it

#

Can you offer an easier way?

slate jasper
#

If you're using list, just use
my_list.pop(index)

#

You can also use
del my_list[index]

boreal wharf
#

i mean. a user will just normally select a UI and deletes it

grand marten
#

a little bit if you have very long lists

slate jasper
grand marten
#

but that is the nature of lists

boreal wharf
grand marten
#

but for short lists in an app like this it shouldn't be any problems

boreal wharf
slate jasper
#

You can you Linked list if you want, but then accessing any particular block will take O(N) time

boreal wharf
#

how can a user delete specific data with index

grand marten
#

you must know the index of the element that you want to delete from the list

boreal wharf
grand marten
#

i wouldn't worry too much about performance of a list in the context of a to-do gui app

slate jasper
boreal wharf
boreal wharf
grand marten
#

but as i said, just forget about the performance aspect of it all

boreal wharf
boreal wharf
grand marten
#

your coding a gui application in python, if performance was a top priority you probably wouldn't be using python in the first place

grand marten
boreal wharf
grand marten
#

instructions from?

boreal wharf
slate jasper
#
# Dictionary to store values
data = {}
key_counter = 1  # To assign incremental keys


def add_value(value):
    global key_counter
    data[key_counter] = value
    key_counter += 1


def delete_value(key):
    if key in data:
        data.pop(key)
    else:
        print(f"Key {key} not found!")

#

Example

add_value("apple")
add_value("banana")
delete_value(1)
print(data)  # {2: 'banana'}
#

This will have better performance

boreal wharf
grand marten
#

it picks the id for new items from the counter

#

the counter always counts up for each item that is added

slate jasper
grand marten
#

when deleting items there will be a gap in the ids, but that shouldn't matter

grand marten
boreal wharf
boreal wharf
grand marten
slate jasper
#
import random
import string

tasks = {}

def generate_id():
    return ''.join(random.choices(string.ascii_uppercase + string.digits, k=4))

def add_task(task):
    task_id = generate_id()
    tasks[task_id] = task
    print(f"Task added: {task_id} -> {task}")
    return task_id

def delete_task(task_id):
    if task_id in tasks:
        removed = tasks.pop(task_id)
        print(f"Task deleted: {task_id} -> {removed}")
    else:
        print(f"No task found with ID {task_id}")


# Example usage
task = input("Add your task: ")
task_id = add_task(task)

print("\nAll tasks:", tasks)

# Example delete call
# delete_task(task_id)
slate jasper
boreal wharf
slate jasper
#

You can make it trigger through frontend using flask or fastapi

boreal wharf
#

Aight

#

thanks

remote hingeBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.