Jan 2, 2013

Remove an element from List in Python


inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]

new_inspiring_ppl = inspiring_ppl[:]

#Note:
#raw_input(), print for Python 2.X Version 
#input(), print() for Python 3.X Version

for person in new_inspiring_ppl:
     print("Do you want to keep person", person, "?")
     #print "Do you want to keep person", person, "?"
     answer = input("yes/no ")
     #answer = raw_input ("yes/no ")
     if answer != "yes":
         inspiring_ppl.remove(person)
 
print(inspiring_ppl)
#print inspiring_ppl


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


No comments:

Post a Comment