Solved

Deleting all members of a list that are of a particular type

Posted on 2006-07-12
1
187 Views
Last Modified: 2010-04-16
Hi there,

Let's say I have a list:

foodlist = ['Fruit', 'Vegetable', 'Fish', 'Pasta', 'Rice_', 'Water', 'Meat_', 'Chocolate']

How would I create a new list that contains members of foodlist that DO NOT end in _  ?    (i.e., the new list would have all members of foodlist except for 'Rice_' and 'Meat_')

newList = ['Fruit', 'Vegetable', 'Fish', 'Pasta', 'Water', 'Chocolate']


newList = []
for food in foodlist:
    if food not ?
         newList.append(food)


It's probably really simple, but I am still novicish (a nice new word!) at Python,


Thanks,
John
0
Comment
Question by:JohnAutoSales
1 Comment
 
LVL 17

Accepted Solution

by:
ramrom earned 500 total points
ID: 17093393
newList = []
for food in foodlist:
    if not food.endswith("_"):
         newList.append(food)

OR

newlist = [food for food in foodlist if not food.endswith("_")]
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python merge rule 4 50
Python script details 2 73
Perl Frameworks 1 80
object oriented programming comparison 5 55
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now