473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

remove an item from string list

I have a string list

items = ['B','D','B','A','E']

Assume that I don't know the order (index) of these items. I would like to remove the second 'B' out of the list without sorting [using items.sort() ] or changing the the order of other items in the list. How can I do that?

The remove() and pop(), only take the first 'B' or I have to know the index of the second 'B' to remove second 'B' out.
Sep 14 '07 #1
12 3906
bartonc
6,596 Expert 4TB
I have a string list

items = ['B','D','B','A','E']

Assume that I don't know the order (index) of these items. I would like to remove the second 'B' out of the list without sorting [using items.sort() ] or changing the the order of other items in the list. How can I do that?

The remove() and pop(), only take the first 'B' or I have to know the index of the second 'B' to remove second 'B' out.
Expand|Select|Wrap|Line Numbers
  1. >>> items = ['B','D','B','A','E']
  2. >>> list(set(items))
  3. ['A', 'B', 'E', 'D']
  4. >>> 
Hmmm... That shouldn't have reordered the list. I'll keep at it..
Sep 14 '07 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> items = ['B','D','B','A','E']
  2. >>> list(set(items))
  3. ['A', 'B', 'E', 'D']
  4. >>> 
Hmmm... That shouldn't have reordered the list. I'll keep at it..
Expand|Select|Wrap|Line Numbers
  1. >>> for i, item in enumerate(items[:]):  #loop through a copy of the list
  2. ...     try:
  3. ...         j = items.index(item, i + 1)
  4. ...         items.pop(j)
  5. ...     except ValueError:
  6. ...         pass
  7. ...     
  8. >>> items
  9. ['B', 'D', 'A', 'E']
  10. >>> 
Sep 14 '07 #3
Expand|Select|Wrap|Line Numbers
  1. >>> items = ['B','D','B','A','E']
  2. >>> list(set(items))
  3. ['A', 'B', 'E', 'D']
  4. >>> 
Hmmm... That shouldn't have reordered the list. I'll keep at it..
I mentioned that I did not know the order of the list in items, I only knew that there was two 'B' in the list. In your case, we would have to know the order of all items.
Sep 14 '07 #4
Expand|Select|Wrap|Line Numbers
  1. >>> for i, item in enumerate(items[:]):  #loop through a copy of the list
  2. ...     try:
  3. ...         j = items.index(item, i + 1)
  4. ...         items.pop(j)
  5. ...     except ValueError:
  6. ...         pass
  7. ...     
  8. >>> items
  9. ['B', 'D', 'A', 'E']
  10. >>> 
Thank you very much. However, is there a simpler way to pop second 'B' out without using loop?
Sep 14 '07 #5
bartonc
6,596 Expert 4TB
Thank you very much. However, is there a simpler way to pop second 'B' out without using loop?
Two tricks here:
1) Start searching for the index one past the location of the first occurrence.
2) Wrap the index() method in a try block in case it fails.
Expand|Select|Wrap|Line Numbers
  1. >>> items = ['B','D','B','A','E']
  2. >>> try:
  3. ...     j = items.index('B', 1)  # one past the location of the first occurrence
  4. ...     items.pop(j)
  5. ... except ValueError:
  6. ...     pass
  7. ... 
  8. >>> items
  9. ['B', 'D', 'A', 'E']
  10. >>> 
Sep 14 '07 #6
Two tricks here:
1) Start searching for the index one past the location of the first occurrence.
2) Wrap the index() method in a try block in case it fails.
Expand|Select|Wrap|Line Numbers
  1. >>> items = ['B','D','B','A','E']
  2. >>> try:
  3. ...     j = items.index('B', 1)  # one past the location of the first occurrence
  4. ...     items.pop(j)
  5. ... except ValueError:
  6. ...     pass
  7. ... 
  8. >>> items
  9. ['B', 'D', 'A', 'E']
  10. >>> 
Thank you. I got the trick
Sep 14 '07 #7
I just thought another way to approach the problem. Could I determine all indexs of 'B' in the list, so when there were numberous 'B's in the list I would be able to pop B at any certain index?
Sep 14 '07 #8
bvdet
2,851 Expert Mod 2GB
I just thought another way to approach the problem. Could I determine all indexs of 'B' in the list, so when there were numberous 'B's in the list I would be able to pop B at any certain index?
I have used this function many times:
Expand|Select|Wrap|Line Numbers
  1. """
  2. Return an index list of all occurrances of 'item' in string/list 's'.
  3. Optional start search position 'i'
  4. """
  5. def indexList(s, item, i=0):
  6.     i_list = []
  7.     while True:
  8.         try:
  9.             i = s.index(item, i)
  10.             i_list.append(i)
  11.             i += 1
  12.         except:
  13.             break
  14.     return i_list
>>> items = ['B','D','B','A','E']
>>> indexList(items, 'B')
[0, 2]
>>>
Sep 14 '07 #9
ilikepython
844 Expert 512MB
I have used this function many times:
Expand|Select|Wrap|Line Numbers
  1. """
  2. Return an index list of all occurrances of 'item' in string/list 's'.
  3. Optional start search position 'i'
  4. """
  5. def indexList(s, item, i=0):
  6.     i_list = []
  7.     while True:
  8.         try:
  9.             i = s.index(item, i)
  10.             i_list.append(i)
  11.             i += 1
  12.         except:
  13.             break
  14.     return i_list
>>> items = ['B','D','B','A','E']
>>> indexList(items, 'B')
[0, 2]
>>>
I don't know about you, but I think this is clearer:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, start = 0):
  2.     i_list = []
  3.     for (i, obj) in enumerate(s[start:]):
  4.         if obj == item:
  5.             i_list.append(i + start)
  6.     return i_list
  7.  
or with map:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, start = 0):
  2.     return filter(lambda x: x, map(lambda (i, obj): i + start if obj == item else None, enumerate(s[start:]))) # i think i over did it
  3.  
I think yours is faster for larger lists though.

EDIT:
List comprhension is definately clearer:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, start = 0):
  2.     return [i + start for (i, obj) in enumerate(s[start:]) if obj == item]
  3.  
Sep 14 '07 #10
bvdet
2,851 Expert Mod 2GB
I don't know about you, but I think this is clearer:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, start = 0):
  2.     i_list = []
  3.     for (i, obj) in enumerate(s[start:]):
  4.         if obj == item:
  5.             i_list.append(i + start)
  6.     return i_list
  7.  
or with map:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, start = 0):
  2.     return filter(lambda x: x, map(lambda (i, obj): i + start if obj == item else None, enumerate(s[start:]))) # i think i over did it
  3.  
I think yours is faster for larger lists though.

EDIT:
List comprhension is definately clearer:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, start = 0):
  2.     return [i + start for (i, obj) in enumerate(s[start:]) if obj == item]
  3.  
Thanks ilikepython. I like your code. I have not done any tests for efficiency. It's good to see alternate methods that accomplish the same goal.
Sep 14 '07 #11
ghostdog74
511 Expert 256MB
I have a string list

items = ['B','D','B','A','E']

Assume that I don't know the order (index) of these items. I would like to remove the second 'B' out of the list without sorting [using items.sort() ] or changing the the order of other items in the list. How can I do that?

The remove() and pop(), only take the first 'B' or I have to know the index of the second 'B' to remove second 'B' out.
go with the basics. The basics are very important
Expand|Select|Wrap|Line Numbers
  1. >>> items = ['B','D','B','A','E']
  2. >>> start=items.index('B') #get the first "B" position
  3. >>> print start
  4.  
  5. >>> second = items[start+1:].index("B") # get the second "B"
  6. >>> print second 
  7. 1
  8. >>> index_to_remove = second + 1 #add 1 to get exact position of second "B" since we started search 1 position after the first "B"
  9. >>> items.pop(index_to_remove)
  10. 'B'
  11. >>> items
  12. ['B', 'D', 'A', 'E']
  13. >>>                            
  14.  
Sep 15 '07 #12
ilikepython
844 Expert 512MB
Thanks ilikepython. I like your code. I have not done any tests for efficiency. It's good to see alternate methods that accomplish the same goal.
The only problem is it doesn't work for patterns. Like, to search for "ACC" in "ACACCAAACC".
Sep 15 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
2
by: Mamatha | last post by:
Hi I have an application with listview.When i click on one button the data will be displayed like this in the listview: colA colB colC ----- ----- ------...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
1
by: dvestal | last post by:
I have a ListView with checkboxes. I want to remove items when the checkboxes are unchecked, but to do so yields an ArgumentOutOfRangeException. Is there an easy way to get around this? A...
4
by: =?Utf-8?B?emhhbmdscg==?= | last post by:
Hi, Following code does not work. (it will crash.)-- I think the reason is that I cannot modify list (call remove) in foreach. But I don't know what I can do. ...
2
by: Steve | last post by:
I am working on a program that works like a check in/check out system. There is a folder on a network drive that stores a bunch of vb programs. This program will check in and check out programs...
10
by: Tony Johansson | last post by:
Hello! I have a collection with a lot of object. Each object has the following definition. public class ParametermappingObj { private string Name {get;set;} private string Id {get;set;}...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.