Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7540823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:44:02+00:00 2026-05-30T07:44:02+00:00

Possible Duplicate: Flatten (an irregular) list of lists in Python I’m trying to use

  • 0

Possible Duplicate:
Flatten (an irregular) list of lists in Python

I’m trying to use the nltk library in python, and more specifically the wordnet corpus, to extract all the words in a broad semantic category like ‘animal’. I’ve managed to write a function that goes down through all the categories and extracts the words in them, but what I end up with is a huge jumble of lists within lists. The lists aren’t of any predictable length or depth, they look like this:

['pet', 'pest', 'mate', 'young', 'stunt', 'giant', ['hen', 'dam', 'filly'], ['head', 'stray', 'dog', ['puppy', 'toy', 'spitz', 'pooch', 'doggy', 'cur', 'mutt', 'pug', 'corgi', ['Peke'], ['chow'], ['feist', 'fice'], ['hound', ['Lhasa', 'cairn']], ['boxer', 'husky']], ['tabby', 'tabby', 'queen', 'Manx', 'tom', 'kitty', 'puss', 'pussy', ['gib']]]

What I want is to be able to grab each of those strings out of that , and return a single, unnested list. Any advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:44:04+00:00Added an answer on May 30, 2026 at 7:44 am

    In general, when you have to deal with arbitrary levels of nesting, a recursive solution is a good fit. Lists within lists, parsing HTML (tags within tags), working with filesystems (directories within directories), etc.

    I haven’t tested this code extensively, but I believe it should do what you want:

    ll = [ 1, 2, 3, [4, 5, [6, 7, 8]]]
    
    def flatten(input_list):
        output_list = []
        for element in input_list:
            if type(element) == list:
                output_list.extend(flatten(element))
            else:
                output_list.append(element)
        return output_list
    
    print (flatten(ll)) #prints [1, 2, 3, 4, 5, 6, 7, 8]
    

    In general recursion is very easy to think about and the solutions tend to be very elegant (like above) but for really, really deeply nested things – think thousands of levels deep – you can run into problems like stack overflow.

    Generally this isn’t a problem, but I believe a recursive function can always* be converted to a loop (it just doesn’t look as nice.)

    • Note: I am not crash-hot on my compsci theory here. Someone can add details or correct me if I’m wrong.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Flatten (an irregular) list of lists in Python How would I go
Possible Duplicate: Flatten (an irregular) list of lists in Python I have the following
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How to Flatten a Multidimensional Array? THe array: itemA => value itemA1
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: how to use foursquare API in android application? This is a question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.