Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-12
1
Medium Priority
?
203 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

916 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