Reputation: 2417
I have a list that is gather from a web scraping script, however I don't need all the values that come back. I would like to pop specific items from the first list and put them into a second list and then use the second list in the rest of the script.
I have the code so that I can do this by the list index. However to make in more versatile, I would like to do by item name. For example, pop just Tag: football
as the first list may change ordering or size.
Current first list:
List1 = [Tag: American Football, Tag: Athletics, Tag: Australian Rules, Tag: Awards, Tag: Badminton, Tag: Baseball, Tag: Basketball, Tag: Bowls, Tag: Boxing, Tag: Cheltenham Festival, Tag: Chess, Tag: Cricket, Tag: Cycling, Tag: Darts, Tag: Football, Tag: Gaelic Games, Tag: Golf, Tag: Greyhounds, Tag: Handball, Tag: Harness Racing, Tag: Horse Racing, Tag: Ice Hockey, Tag: Motorsport, Tag: Novelty, Tag: Politics, Tag: Pool, Tag: Rugby League, Tag: Rugby Union, Tag: Snooker, Tag: TV, Tag: Table Tennis, Tag: Tennis, Tag: UFC/MMA, Tag: Volleyball, Tag: Winter Sports]
Expected Second list:
List2 = [Tag: Football, Tag: Horse Racing]
Code:
List1 = [Tag: American Football, ...]
List2 = []
List2.append(List1.pop(15))
Upvotes: 0
Views: 1179
Reputation: 165
I would suggest it depends on the library you are using.
Seems like this Tag
type is a class that comes within it.
You have to look through the list searching the object you need accessing its attribute, appending this object to a second list and removing from the first.
Example:
def custom_pop(list1: list, value):
obj = next((tag for tag in list1 if tag.ATTRIBUTE == value), None)
if obj:
list1.remove(obj)
return obj
After that append this obj to a second list.
Upvotes: 1