Skip to content

Feat/flatten array python solution#22

Open
alangnt wants to merge 2 commits intocodedex-io:mainfrom
alangnt:feat/flatten-array-python-solution
Open

Feat/flatten array python solution#22
alangnt wants to merge 2 commits intocodedex-io:mainfrom
alangnt:feat/flatten-array-python-solution

Conversation

@alangnt
Copy link
Copy Markdown
Contributor

@alangnt alangnt commented Mar 26, 2026

Hi Codédex team,

This PR adds a working Python solution for today's Flatten Array 🪜 challenge, and updates the README.md file to add today's GitHub link.

Here's the code :

def flatten(lst):
  # first let's init a var for the flattened array
  flat_array = []

  # for each item in the list
  for i in lst:
    # if we detect another array inside of it
    # we re-apply the function recursively
    if isinstance(i, list):
      flat_array.extend(flatten(i))
    # otherwise we simply append it
    else:
      flat_array.append(i)

  # finally we return the flattened array
  return flat_array

Here's how it works :

  • init a flat_array var
  • for each item in the list
  • we use isinstance to detect if it's an item or another array
  • if an array, we recursively call the function and extend the result
  • otherwise we simply append the item
  • finally we return the flattened array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant