Python

Mason Long
Mason Long

How do I split a list like I would a string?
I want to extract everything after the word "title".

Attached: wut.gif (155 KB, 220x171)

Elijah Taylor
Elijah Taylor

I thought slicing would work but you can't specify the string

Julian Lewis
Julian Lewis

Take string's length and slice after

Jayden Williams
Jayden Williams

regex

Oliver King
Oliver King

Its actually a dictionary element

Luis Allen
Luis Allen

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

William Price
William Price

string.substring(string.indexof("title"), string.length);

Daniel Clark
Daniel Clark

WTF is this

Attached: Screen-Shot-2019-06-12-at-12.44.27-AM.png (290 KB, 2528x494)

James Sullivan
James Sullivan

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)

Xavier Foster
Xavier Foster

This will print out everything in the list after 'title'

['three', 'four']

Make it into a function and it should get the job done

Ayden Peterson
Ayden Peterson

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?

Caleb Wright
Caleb Wright

the_list[the_list.index("title")+1:]
Don't listen to these other rooty poots

Gavin Miller
Gavin Miller

Dang. I did this one which is much longer, but readable so that's cool. Yours are better though

Bentley Gutierrez
Bentley Gutierrez

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.")

Gavin Cruz
Gavin Cruz

It said could not find title in the list

Attached: giphy.gif (988 KB, 245x160)

Sebastian Myers
Sebastian Myers

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?

Robert Nelson
Robert Nelson

Look at my screenshot. At the end of the second line is title

Ryder Rodriguez
Ryder Rodriguez

That's a key in a dictionary embedded as an element in a list

Andrew Taylor
Andrew Taylor

Looks like you're dealing with a dictionary not a list dude.

Wyatt Howard
Wyatt Howard

I said it was a dictionary element.

I typed print(mylist["Title"]) and it says "list indices must be integers or slices, not str"

David Sanders
David Sanders

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.

Attached: tenor.gif (1.35 MB, 430x180)

Jaxson Gray
Jaxson Gray

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": ""}'

Aaron Parker
Aaron Parker

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?

Kevin Wood
Kevin Wood

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])

William Diaz
William Diaz

You win one hilarious webm

Attached: 1560313302363.webm (2.74 MB, 480x360)

Tyler Gonzalez
Tyler Gonzalez

//boards.Jow Forums.org/pol/thread/216048960#p216048960
Link to thread if it doesn't work

Kevin Hill
Kevin Hill

Congrats buddy. Enough of us retards put together we'd be dangerous haha

Robert Wright
Robert Wright

Perhaps I will use this to study the genetics of weaponized autism

Asher Turner
Asher Turner

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.

Ian Morales
Ian Morales

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.

Jace Roberts
Jace Roberts

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)

Lincoln Bennett
Lincoln Bennett

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?

Michael James
Michael James

Notice that the ID list
id= "26161435, 27416076, 21081501
the numbers don't have colons

Logan Edwards
Logan Edwards

Attached: Screen-Shot-2019-06-12-at-3.00.07-AM.png (79 KB, 2524x184)

Adam Stewart
Adam Stewart

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.

Isaac Roberts
Isaac Roberts

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

Parker Murphy
Parker Murphy

Ignore the green texted part there, I meant to edit that out before posting

Christopher Richardson
Christopher Richardson

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

Leo Jackson
Leo Jackson

No problem man. Good luck with your project

Leo Kelly
Leo Kelly

good thread guys im proud of you

Disable AdBlock to view this page

Disable AdBlock to view this page

Confirm your age

This website may contain content of an adult nature. If you are under the age of 18, if such content offends you or if it is illegal to view such content in your community, please EXIT.

Enter Exit

About Privacy

We use cookies to personalize content and ads, to provide social media features and to analyze our traffic. We also share information about your use of our site with our advertising and analytics partners.

Accept Exit