Talk about a wild year. A former reality TV star became president. Decades-long careers ended amid an avalanche of sexual misconduct allegations. News cycles moved faster than a Twitter feed.

Basically, late-night hosts had a lot to work with in 2017.

The comedy shows they helm spent the year turning all those headlines into punchlines, scrambling to make last-minute changes to monologue jokes or give viewers breaks from politics with utterly kooky segments.

So we asked writers of the top daily late-night shows to pick some of their favorite sketches, jokes or moments of the year and explain how they came to be.

Below are their answers, written via email or recounted over the phone.

THE TONIGHT SHOW STARRING JIMMY FALLON

Jessica Chastain in a sketch on "The Tonight Show Starring Jimmy Fallon."

Jessica Chastain in a sketch on "The Tonight Show Starring Jimmy Fallon."

Guest Jessica Chastain has been vocal about the treatment of women in Hollywood, and "after talking for a while about all the sexism we have seen in the industry, this idea seemed to just appear," said writers Jasmine Pierce and Taryn Englehart.

In this sketch, Chastain repeats a simple line during a fake casting call but impressively shifts her delivery based on the director's sexist requests. ("Can you try it again? But hot.").

"The jokes that were pitched to our producers were almost exactly the ones that ended up in the sketch because the idea just seemed to come so immediately," the writers said. "It's always exciting to be able to write for women, but especially when you get to shed light on something women struggle with every day."

In a twist on Fallon's recurring "Thank You Notes" segment - which usually features him writing funny notes of gratitude to music - the show's female writers came on air to thank Hillary Clinton in person.

"Thank you, Hillary, for all the work you've done for public health care," one writer said. "Ever since the election, I've really depended on my government-subsidized anti-anxiety medication."

Pierce and Englehart referred to this as "our favorite segment of the year and probably moment of our lives so far." They continued: "We were brainstorming Hillary ideas, and we said we couldn't think of anything because all we wanted to do was scream and cry and tell her thank you. That's when it hit us: 'Thank You Notes.' We reached out to the other female writers, asking everyone to pitch jokes for them to read."

THE LATE SHOW WITH STEPHEN COLBERT

"The Late Show with Stephen Colbert" went live after President Trump's speech to the joint houses of Congress.

"The Late Show with Stephen Colbert" went live after President Trump's speech to the joint houses of Congress.

Colbert hosted a live show after President Donald Trump concluded his first joint address to Congress, which gave "The Late Show" about an hour to write a monologue.

"Imagine getting all of your friends together in one room, then sitting there quietly trying to think of jokes while an angry old man yells at them," wrote Opus Moreschi and Jay Katsir on behalf of "The Late Show" writers. Former Obama speechwriter Josh Earnest, a guest that night, sat with writers as they watched Trump, offering insights and even pitching a few jokes.

The show has developed a system for live shows, they continued: "Writers furiously come up with jokes in real-time in a massive shared document (using Scripto, the cloud-based collaborative TV writing software that Stephen developed in his spare time. Not a joke.). The head writers sift through everything that's written, and arrange the best material into a narrative. While producers and our footage department furiously scramble to get everything together, Stephen chooses the jokes he likes (and, frustratingly, often comes up with much better jokes). Sometimes the script gets finished mere minutes before we go live, and after we pull it off, the whole staff has a celebratory ulcer."

This recurring segment features Colbert as the country's TV dad calling a family meeting and, with a guest, giving some fatherly advice to America. But this edition, in which he and Nick Kroll (playing "America's Cool Older Cousin Nick") give "the talk" about puberty, in part inspired the viral #PuberMe challenge, which had celebrities sharing awkward puberty photos. The effort raised $1 million for Puerto Rico hurricane relief.

Kroll "made a lot of great additions to the script, some of which surprised and delighted Stephen during the taping, who at times struggled to keep a straight face," Moreschi and Katsir wrote. "It's always exciting to see Stephen laugh on camera, partially because, as writers, we have a Pavlovian response to it."

Jimmy Kimmel became the late-night face of the health-care debate this year with an emotional monologue about his newborn son Billy's heart condition. The plea from Kimmel to protect people with pre-existing conditions inspired Sen. Bill Cassidy, R-La., to create "The Jimmy Kimmel Test" for future legislation, which "surprised and honored" the show, said co-head writer Molly McNearney.

But Cassidy's subsequent proposal drew heat from Kimmel, who said on air that the senator had failed his own test. "We had to hold him accountable," said McNearney, who is also Kimmel's wife and Billy's mother. "Jimmy's monologue resonated - not just with our audience, but with Washington. We felt like we were doing a service to kids and veterans and the sick who get sidelined. We've never gotten that feeling from a Kardashian joke."

Ben Affleck and J.J. Abrams present a fake movie trailer based on a comic Jimmy Kimmel drew as a kid.

Ben Affleck and J.J. Abrams present a fake movie trailer based on a comic Jimmy Kimmel drew as a kid.

Kimmel hates his birthday, but his 50th fell on a workday - so the show surprised him with segments in his honor featuring celebrity friends, including a George Clooney-narrated retrospective and Adam Sandler and Ray Romano delivering the monologue.

One highlight: Ben Affleck and J.J. Abrams presenting a fake movie trailer based on a comic Kimmel drew as a kid. Kimmel called it " 'the best gift he's ever received.' Jimmy is still talking about that trailer," wrote McNearney and co-head writer Danny Ricker.

"Giving him a day where he didn't have to write a monologue was the best present," they added. "Jimmy actually got to go to breakfast that morning. He's never done that in 15 years at the show. And he'll never do it again so (we) hope the pancakes were good."

"Around the time of the Oscars we typically do a roundup, where we slightly alter scenes from nominated movies - changing audio, inserting a shot, etc.," Todd Levin wrote. "I was sort of obsessed with a very inconsequential moment in 'La La Land.' "

Ryan Gosling sits on a pier and finds a fedora, then walks past a couple and gives the hat to the man. Levin, frustrated by the randomness of the scene, "pitched something that ended up being one of the most ambitious - and stupid - shoots I've ever done at Conan."

The prerecorded short featured the couple, who are black, showing the hat to a friend. "It's bad luck," the man says. "Don't you know when a white boy puts a hat on you, that's a curse?" the woman adds. The fedora ends up haunting them.

"We actually found and cast the original actors from this scene in 'La La Land,' but we had to recast them at the very last minute because their religious beliefs forbid them from participating in a sketch that dabbles in the supernatural," Levin added. "That has happened to us more times than I care to remember."

One day in 2016, as writer Andrés du Bouchet made changes to a comedy bit and picked at the snack table, he saw "a small paper plate with nuts and just a plastic teaspoon sitting on it," he recalled. "OK, so I'm supposed to use this spoon to pick up the nuts? And I realized it was next to impossible."

He asked writer Dan Cronin to try, telling him, "This would be a good starting point for something really dumb," du Bouchet said. "That's kind of how a lot of comedy bits come about: a kernel of an idea that's seemingly innocuous and how can we spin it out of control."

Du Bouchet eventually wrote a long sketch with him playing a cameraman complaining to Conan O'Brien about "the nut spoon." It didn't make it to air at first, but then another episode was light on comedy. "Conan was at his desk and said, 'Well, I guess it's time for nut spoon,' " du Bouchet said. "All the interns started chanting, 'Nut spoon, nut spoon!' "

A week later, comedy legend Carl Reiner told O'Brien, "It was the best sketch I've seen in a long time. I play it for friends." Du Bouchet said the compliment "pretty much made my year."

THE DAILY SHOW WITH TREVOR NOAH

Trevor Noah on Pennsylvania Representative Tim Murphy, who voted on a bill to ban abortions after 20 weeks, resigns when it's revealed he pressured his mistress to terminate her pregnancy.

Trevor Noah on Pennsylvania Representative Tim Murphy, who voted on a bill to ban abortions after 20 weeks, resigns when it's revealed he pressured his mistress to terminate her pregnancy.

Staunchly antiabortion Rep. Tim Murphy, R-Pa., became embroiled in scandal in October when news reports said he had asked the woman with whom he had had an extramarital affair to have an abortion.

"It was so beautifully hypocritical," said writer Kat Radley. In amorning meeting, writers watched a clip of Murphy saying he was going to finish his time in office but not seek re-election. "I then said something to the effect, 'Oh, well that's nice he's allowing his time in office to come to term.' It eventually became the joke on which we decided to end."

But later in the day, "we were thrown a curveball" when Murphy announced he'd actually end his tenure early. "But we were still able to use the metaphor by changing the punchline to say, 'It's such a pity Tim Murphy terminated his career before it came to term.' "

That joke, which went viral the following day, "was a proud moment for me as a new writer," Radley added.

Before Trump's inauguration, few celebrities had signed up to join, and an organizer predicted it would be characterized by "a soft sensuality." The topic seemed perfect for regular commentator Lewis Black.

"The idea was that Lewis was angry about the inauguration fiasco because he had tickets," Zach DiLanzo wrote. "But we had to figure out a reason why he would be attending." He came up with: "I'm going no matter what, because I want to be there when Trump touches the Bible and his hand catches on fire."

Co-writer Dan McCoy wrote this joke: "Right now, the two biggest acts booked for the inauguration are an 'America's Got Talent' contestant - contestant, not winner! - and the Mormon Tabernacle Choir. Oh, yes! The thrill of choir music, but with the edginess of Mormonism!"

"The pleasure of writing Lewis Black segments is that his voice is so clear - barely concealed rage," McCoy said. "It's fun to pitch to Lewis, because it's impossible to write jokes for him without doing a Lewis Black impression. So you wind up doing Lewis Black to Lewis Black."

LATE NIGHT WITH SETH MEYERS

Amber Ruffin talks about her safe space on "Late Night with Seth Meyers."

Amber Ruffin talks about her safe space on "Late Night with Seth Meyers."

In the middle of a particularly depressing news cycle of racist protests, writer Amber Ruffin recalled,"I had come to the end of my rope, honestly. I didn't want to talk about anything."

So, she thought, "what if I took Seth to my safe space, a space I had created for myself, where white people couldn't touch your hair and you couldn't mention any freaking, horrible news stories?"

She and Jenny Hagel wrote a sketch about Ruffin taking refuge in a room painted pink and full of her favorite things: soft carpeting, cake, margaritas and pictures of Rep. Maxine Waters, D-Calif.

The room was built on the stage of "Saturday Night Live," and Ruffin even spent downtime there on the day of taping. "It ended up being this really lovely, beautiful place," she said. "It was an odd thing to do, not a normal late-night show sketch ... It was also nice to say to people, 'Hey, look, it's tough to be black right now, and you don't have to be tough all the time. You can sit down and have a margarita.' "

Kevin Spacey came out as gay in a statement that addressed allegations that he made a sexual advance on a minor. Like others in the LGBT community, Hagel was very angry.

"It hit me in a specific way," she said. "The idea that gay people are pedophiles, it's a stereotype."

While she wasn't assigned to write a segment about it, the next morning, she happened to score a seat on the F train and have her laptop handy. She opened it and wrote out the flood of feelings.

Hagel showed the pages to Ruffin, and, together, they wrote out the segment; two days later, Hagel sat on a stool during a taping and ran through her anger with Spacey's statement, point by point - laced with jokes, of course.

"Some days, your biggest contribution is a bee wearing a hat - and that's fun and always has value. But it's also important to channel something I care about into my work."

THE LATE LATE SHOW WITH JAMES CORDEN

"Stranger Things" kids as a Motown Super Group on "The Late Late Show wth James Corden."

"Stranger Things" kids as a Motown Super Group on "The Late Late Show wth James Corden."

James Corden came up with the idea of singing a Motown medley with the stars of Netflix's "Stranger Things," and writers developed a mockumentary-type sketch about the faux singing group's backstory. They went through three or four drafts with Corden, aiming for a fast-paced sketch to get to the performance quickly.

"We also had a very short time with the kids to rehearse the songs, learn the choreography, shoot the sketch and talking heads, then shoot the Motown medley live in front of an audience - so the script had to be tight," Nedaa Sweiss said.

The final package became a viral hit (8 million YouTube views and counting) and revealed the kids' singing chops.

Corden's recurring "Role Call" conceptis "the go-to sketch whenever we have a major movie star on the show," said Lauren Greenberg, who writes them with comedy producer Kate Presutti. With Corden, the stars re-create their iconic movie scenes in rapid-fire succession.

For "Role Call," Greenberg and Presutti spend every Friday for several weeks watching movies, writing scenes on note cards and shuffling them around on a bulletin board before pitching the selections and jokes to Corden and executive producers.

"It's a giant puzzle, and I live for the moments when we figure out that the props and costumes from the beauty shop in 'Steel Magnolias' transition perfectly into the bathroom vanity scene in 'Charlie Wilson's War,' " Greenberg said.

The Roberts edition - the first with a female star - inspired a lot of debate, since her movies have multiple iconic scenes. " 'Pretty Woman' was obviously the hardest to narrow down," Greenberg said. But after they talked it out, they landed on Greenberg's choice, the "big mistake" shopping scene. "I think that might be the only debate I've ever won," she said.