?
Solved

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

Posted on 2006-07-12
1
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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…

719 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