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

  • Home
  • SEARCH
  • 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 8834959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:08:53+00:00 2026-06-14T09:08:53+00:00

Possible Duplicate: Flatten (an irregular) list of lists in Python Example 1: Lets say

  • 0

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

Example 1:

Lets say I have a list: [[1,2], [3,4]]. I could use two for loops to be able to print out: 1, 2, 3, 4.

Example 2:

So, now let’s suppose that I’m given an output and I don’t know how many nested lists there within list1:

list1 = [1, [1, 2, [3, 5, 6,[ .. ], …, ] ] ] ] ]

So, my question is how would I be able to print out every individual number in the same format as the first example. I’m working with something right now that gives me nested lists as a result, but different inputs to the function will give me different amount of nested lists.

What I can think of is to do this, but I don’t know what to do after the isinstance part:

c = 0
for i in list1:
   while c < len(list1):
         if isinstance(i, list):

         else:
              print i
         c += 1

Thanks

First Edit

If there is also a way to deconstruct all the nested lists to become a single list that would work as well for me, but I’m curious to know answers to both these problems.

  • 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-06-14T09:08:55+00:00Added an answer on June 14, 2026 at 9:08 am

    The itertools documentation has some pretty good examples of iterating over lists and such, so it’s always a good place to start when faced with such a task.

    I would recomend using a generator, which avoid creating many levels of lists:

    def flatten_all(iterable):
        for elem in iterable:
            if not isinstance(elem, list):
                yield elem
            else:
                for x in flatten_all(elem):
                    yield x
                # in Python 3.3 just: yield from flatten_all(elem)
    

    Application:

    for x in flatten_all([1, [2, [3]]]):
        print(x)
    
    # or if you need a list:
    my_lst = list(flatten_all([1, [2, [3]]])
    assert my_lst == [1, 2, 3]
    

    Edit: Non-recursive linear version

    def flatten_all(iterable):
        stack = [iter(iterable)]
        while stack:
            try:
                elem  = stack[-1].next()
                if not isinstance(elem, list):
                    yield elem
                else:
                    stack.append(iter(elem))
            except StopIteration:
                stack.pop()
    
    • 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 I'm trying to use
Possible Duplicate: Flatten (an irregular) list of lists in Python I have a python
Possible Duplicate: Flatten (an irregular) list of lists in Python I have the following
Possible Duplicate: Flatten (an irregular) list of lists in Python I have a list
Possible Duplicate: Flatten (an irregular) list of lists in Python How would I go
Possible Duplicate: Flatten (an irregular) list of lists in Python for instance, from: [[1,2,3],'a',[[4],[5,6],7]]
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Cyclic module dependencies and relative imports in Python Consider the following example
Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: Trying to get tables next to each other horizontal I have two

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.