How do I split a list like I would a string?
I want to extract everything after the word "title".
Python
I thought slicing would work but you can't specify the string
Take string's length and slice after
regex
Its actually a dictionary element
Iterate through the list, check for "title" on each iteration, once you do use the found index to get everything after it.
Or use the join method and then just get the relevant shet as a string, then split it again lol
string.substring(string.indexof("title"), string.length);
WTF is this
your_list = ['one', 'two', 'title', 'three', 'four']
extracted_items = []
reached_title = False
for item in your_list:
if reached_title:
extracted_items.append(item)
elif item == 'title':
reached_title = True
print(extracted_items)
This will print out everything in the list after 'title' ['three', 'four']
Make it into a function and it should get the job done
try:
mylist = ["dog", "title", "foo", "bar"]
newlist = mylist[mylist.index("title")+1:]
except ValueError:
print("Could not find 'title' in the list.")
newlist will now contain:
['foo', 'bar']
Is that what you wanted?
the_list[the_list.index("title")+1:]
Don't listen to these other rooty poots
Dang. I did this one which is much longer, but readable so that's cool. Yours are better though
Forgot tags. Also, I didn't mean to put mylist into the "try ... except" block.
mylist = ["dog", "title", "foo", "bar"]
try:
newlist = mylist[mylist.index("title")+1:]
except ValueError:
print("Could not find 'title' in the list.")
It said could not find title in the list
Beautiful.
Why don't you paste your list in. Are you saying the word "title" is in the list but not necessarily by itself as an element?
Look at my screenshot. At the end of the second line is title
That's a key in a dictionary embedded as an element in a list
Looks like you're dealing with a dictionary not a list dude.
I said it was a dictionary element.
I typed print(mylist["Title"]) and it says "list indices must be integers or slices, not str"
So your saying I should KMS? Thats why this is fucking me in the a hole, I don't know whether its a list or dictionary.
There is always a way
the_data = {"a":"b", "c":"d", "Title":"Molecular bullshit", "volume": ""}
import json
json.dumps(the_data).split("Title")[1]
'": "Molecular bullshit", "volume": ""}'
When you say you "need everything after Title" what are you asking for exactly? You have a list of dictionaries. Each dictionary in your list has a title. You could get the first one like so:
the_data[0]["Title"]
And you would get that title. Is that what you're looking for?
You get all the titles like this:
for d in data:
print(d["Title"])
To get the keys and items for each dictionary, do this:
for d in data:
for k in d.keys():
print(k, d[k])
You win one hilarious webm
Link to thread if it doesn't work
Congrats buddy. Enough of us retards put together we'd be dangerous haha
Perhaps I will use this to study the genetics of weaponized autism
Cool man. If you have any more questions, post them in this thread. I've been doing this Python thing for a while and I'm feeling helpful tonight. Though I am about to step out for 30 minutes or so.
Why Can't I use [0:1]["Title"]?
I just typed: print(read[0]["Title"]+read[1]["Title"]) but if I want to print lots of data, that won't be very efficient.
I'm back
When you index a list, it returns just the element at that index from the list so data[0] returns the first dictionary in your case.
However, slicing like data[0:1] is not the same as indexing. When you slice, it returns a list of the elements starting at the first index of your slice and stopping 1 before the second index. Since your slice is [0:1], it starts at 0 and ends at 0 so you only get the first element. But because it's a slice, it still gets returned inside of a list.
>I just typed: print(read[0]["Title"]+read[1]["Title"]) but if I want to print lots of data, that won't be very efficient.
Try this:
titles = [e["Title"] for e in read]
print(titles)
>titles = [e["Title"] for e in read]
>print(titles)
You're 2/2. Nice, All though IDK what you did
Last Q
In order to get these titles, I have to write this: id= "26161435, 27416076, 21081501"
However I get an ID list which is a list of intgers. Problem is, is that each integer has colons around it making it a string, I.E. '30516450', '30381971', '30129371', '29550974', '29542350', '29437900'.
How can I convert the list into integers to insert into the ID section?
Notice that the ID list
> id= "26161435, 27416076, 21081501
the numbers don't have colons
>Yours are better though
As someone who regularly has to dig through shitty code written by other people - unless performance is crucial, I will take readable over "better" every single time.
>> id_list = ['30516450', '30381971', '30129371', '29550974', '29542350', '29437900']
>>> id_list
id_list = ['30516450', '30381971', '30129371', '29550974', '29542350', '29437900']
id_list = ",".join(id_list)
print(id_list)
That oughta fix you right up
Ignore the green texted part there, I meant to edit that out before posting
That sounds right but well have to wait till i can plug in my laptop lol gotta sleep and its downstairs.
Thanks again tho, this has been 10× better than stackoverflow. They have 0 patience for noobs
No problem man. Good luck with your project
good thread guys im proud of you