954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to use del

"del" allows you to delete elements from a list for example...

# Makes list, note in python when addressing an element, the first element is 0, the second is 1. 

a = [1,2,3,4]
#   [0,1,2,3] shows how python "reads" the location.


# Prints elements in list a
print a

# deletes the "first" element which is element 0
del a[0]

# prints the elements in list a, minus the original 1 value in the 0 place.
print a


Lots more specifics at http://learnpythonthehardway.org/book/ex34.html

Cenchrus
Newbie Poster
19 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

You can expand that with slicing ...

# using del and slicing on a list

ar = range(7)

print ar  # [0, 1, 2, 3, 4, 5, 6]

# delete element 1 and 2
del ar[1:3]

print ar  # [0, 3, 4, 5, 6]

ar = range(7)

# delete every second element
del ar[::2]

print ar  # [1, 3, 5]
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: