How to convert a nested list into a one-dimensional list in Python? [duplicate]

You need to recursively loop over the list and check if an item is iterable(strings are iterable too, but skip them) or not. itertools.chain will not work for [1,[2,2,2],4] because it requires all of it’s items to be iterable, but 1 and 4 (integers) are not iterable. That’s why it worked for the second one … Read more

Consistent Styling for Nested Lists with Bootstrap

Nested Group Lists .just-padding { padding: 15px; } .list-group.list-group-root { padding: 0; overflow: hidden; } .list-group.list-group-root .list-group { margin-bottom: 0; } .list-group.list-group-root .list-group-item { border-radius: 0; border-width: 1px 0 0 0; } .list-group.list-group-root > .list-group-item:first-child { border-top-width: 0; } .list-group.list-group-root > .list-group > .list-group-item { padding-left: 30px; } .list-group.list-group-root > .list-group > .list-group > .list-group-item … Read more

2D list has weird behavor when trying to modify a single value [duplicate]

This makes a list with five references to the same list: data = [[None]*5]*5 Use something like this instead which creates five separate lists: >>> data = [[None]*5 for _ in range(5)] Now it behaves as expected: >>> data[0][0] = ‘Cell A1’ >>> print(data) [[‘Cell A1’, None, None, None, None], [None, None, None, None, None], … Read more

Python append() vs. + operator on lists, why do these give different results?

To explain “why”: The + operation adds the array elements to the original array. The array.append operation inserts the array (or any object) into the end of the original array, which results in a reference to self in that spot (hence the infinite recursion in your case with lists, though with arrays, you’d receive a … Read more