Skip to content

Instantly share code, notes, and snippets.

@Codehunter-py
Created July 25, 2022 14:26
Show Gist options
  • Save Codehunter-py/af5b8896d5d3d30b9f4bae757ab36e9f to your computer and use it in GitHub Desktop.
Save Codehunter-py/af5b8896d5d3d30b9f4bae757ab36e9f to your computer and use it in GitHub Desktop.
Given a 2D list, write a Python program to convert the given list into a flattened list.
import numpy
list_2d = [['Volkswagen', 'Mercedes', 'BMW'], ['Honda', 'Toyota', 'Mazda']]
def convertList(list_2d):
return list(numpy.concatenate(list_2d).flat)
print(convertList(list_2d)) # Output ['Volkswagen', 'Mercedes', 'BMW', 'Honda', 'Toyota', 'Mazda']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment