'for loop to list comprehension[nested for loop)
After a few tries I am not able to convert this for loop into a single line[list comprehension]
print(cells_don[0])
cells_v_don = list()
for cell in cells_don:
row = list()
for i in range(0, len(cell)):
row.append(cell[i].value)
cells_v_don.append(row)
Output: It s just a tuple collecting data from a excel table:
(<Cell 'ede_ser'.C13>, <Cell 'ede_ser'.D13>, <Cell 'ede_ser'.E13>, <Cell 'ede_ser'.F13>, <Cell 'ede_ser'.G13>, <Cell 'ede_ser'.H13>, <Cell 'ede_ser'.I13>, <Cell 'ede_ser'.J13>, <Cell 'ede_ser'.K13>, <Cell 'ede_ser'.L13>, <Cell 'ede_ser'.M13>, <Cell 'ede_ser'.N13>, <Cell 'ede_ser'.O13>, <Cell 'ede_ser'.P13>, <Cell 'ede_ser'.Q13>, <Cell 'ede_ser'.R13>, <Cell 'ede_ser'.S13>, <Cell 'ede_ser'.T13>)
How can I convert this code into a list comprehension?
Thank you!
Solution 1:[1]
cells_v_don = [
[cell[i].value for i in range(0, len(cell))]
for cell in cells_don
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ForceBru |
