Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
I need to declare an Array and all the items present in the ListBox Should Be erased independent of the Group name present in the ListBox. can anyone help me coding in Python. I'm utilizing WINXP OS and Python 2.6.

1 Answer

0 votes
by (26.4k points)

In python, list is actually a dynamic array. You can create them like below:

lst = [] # Declares an empty list named lst

You can also fill it with items:

lst = [1,2,3]

With "append", we can add items

lst.append('a')

You can also iterate the elements of the list, with the help of for loop:

for item in lst:

    # Do something with item

If you want to keep the track on the current index:

for idx, item in enumerate(lst):

    # idx is the current idx, while item is lst[idx]

To eliminate elements, you can utilize the del order or the remove functions as in: 

del lst[0] # Deletes the first item

lst.remove(x) # Removes the first occurence of x in the list

Note, however, that one can't repeat over the list and change it simultaneously; to do that, you ought to rather iterate over a cut of the list (which is fundamentally a duplicate of the list). As in:

for item in lst[:]: # Notice the [:] which makes a slice

       # Now we can modify lst, since we are iterating over a copy of it

Looking for a good python tutorial course? Join the python certification course and get certified.

Related questions

0 votes
1 answer
asked Oct 11, 2019 in Python by Sammy (47.6k points)
0 votes
2 answers
0 votes
1 answer
asked Jul 8, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Mar 11, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...