Questions tagged [flatten]

Flattening refers to either reducing a multi-dimensional array to a single dimension or to reducing a class and class methods to handle based function calls.

1554 questions
5192
votes
33 answers

How do I make a flat list out of a list of lists?

I have a list of lists like [ [1, 2, 3], [4, 5, 6], [7], [8, 9] ] How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]? If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly…
Emma
  • 52,713
  • 4
  • 19
  • 10
1532
votes
87 answers

Merge/flatten an array of arrays

I have a JavaScript array like: [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]] How would I go about merging the separate inner arrays into one like: ["$6", "$12", "$25", ...]
Andy
  • 18,723
  • 12
  • 46
  • 54
550
votes
51 answers

Flatten an irregular (arbitrarily nested) list of lists

Yes, I know this subject has been covered before: Python idiom to chain (flatten) an infinite iterable of finite iterables? Flattening a shallow list in Python Comprehension for flattening a sequence of sequences? How do I make a flat list out of a…
telliott99
  • 7,762
  • 4
  • 26
  • 26
430
votes
3 answers

What is the difference between flatten and ravel functions in numpy?

import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two…
cryptomanic
  • 5,986
  • 3
  • 18
  • 30
372
votes
31 answers

How to Flatten a Multidimensional Array?

Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references? I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of array_map() and array_values().
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
258
votes
8 answers

Turn Pandas Multi-Index into column

I have a dataframe with 2 index levels: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 …
TheChymera
  • 17,004
  • 14
  • 56
  • 86
237
votes
5 answers

How to flatten only some dimensions of a numpy array

Is there a quick way to "sub-flatten" or flatten only some of the first dimensions in a numpy array? For example, given a numpy array of dimensions (50,100,25), the resultant dimensions would be (5000,25)
IssamLaradji
  • 6,637
  • 8
  • 43
  • 68
71
votes
4 answers

How to flatten a pandas dataframe with some columns as json?

I have a dataframe df that loads data from a database. Most of the columns are json strings while some are even list of jsons. For example: id name columnA columnB 1 John {"dist": "600", "time":…
sfactor
  • 12,592
  • 32
  • 102
  • 152
70
votes
4 answers

Is it possible to flatten MongoDB result query?

I have a deeply nested collection in my MongoDB collection. When I run the following query: db.countries.findOne({},{'data.country.neighbor.name':1,'_id':0}) I end up with this nested result here: {"data" : { "country" : [ { "neighbor"…
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
62
votes
12 answers

Flatten a javascript object to pass as querystring

I have a javascript object that I need to flatten into a string so that I can pass as querystring, how would I do that? i.e: { cost: 12345, insertBy: 'testUser' } would become cost=12345&insertBy=testUser I can't use jQuery AJAX call for this call,…
Saxman
  • 5,009
  • 11
  • 51
  • 72
56
votes
10 answers

How to flatten array in jQuery?

How to simply flatten array in jQuery? I have: [1, 2, [3, 4], [5, 6], 7] And I want: [1, 2, 3, 4, 5, 6, 7]
extern.fx
  • 643
  • 1
  • 5
  • 9
56
votes
14 answers

JavaScript flattening an array of arrays of objects

I have an array which contains several arrays, each containing several objects, similar to this. [[object1, object2],[object1],[object1,object2,object3]] Here is a screenhot of the object logged to the console. What would be the best approach to…
byrdr
  • 5,197
  • 12
  • 48
  • 78
54
votes
7 answers

How to flatten a list to a list without coercion?

I am trying to achieve the functionality similar to unlist, with the exception that types are not coerced to a vector, but the list with preserved types is returned instead. For instance: flatten(list(NA, list("TRUE", list(FALSE), 0L)) should…
eold
  • 5,972
  • 11
  • 56
  • 75
46
votes
12 answers

Flatten array with objects into 1 object

Given input: [{ a: 1 }, { b: 2 }, { c: 3 }] How to return: { a: 1, b: 2, c: 3 } For arrays it's not a problem with lodash but here we have array of objects.
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
44
votes
12 answers

How to "flatten" or "index" 3D-array in 1D array?

I am trying to flatten 3D array into 1D array for "chunk" system in my game. It's a 3D-block game and basically I want the chunk system to be almost identical to Minecraft's system (however, this isn't Minecraft clone by any measure). In my previous…
user925777
1
2 3
99 100