MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
September
Sun Mon Tue Wed Thu Fri Sat
         
12
2017
Months
Sep


Tue, Sep 12, 2017 10:58 pm

Adding an item to a list in Python without duplicates

If you have a list in a Python script and wish to add new items to the list, you can use listname.append(newitem) where listname is the name of the list and newitem is the item you wish to add to the list. If you wish to add new items to the list only if they don't already exist in the list, you can avoid adding an existing entry to the list by checking if the entry is already in the list as shown below:

filenameList = []
if filename not in filenameList:
     filenameList.append(filename)

If you wish to know the length of a list, i.e., how many items are on the list, you can use print len(listname) where listname is the name of your list.

[/languages/python] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo