How to convert list of lists into list -python

Question:

I need to convert a list of lists (containing strings) into a simple list of integers.
I have

mylist = [["14"],["2"],["75"],["15"]]

I need

newlist = [14, 2, 75, 15]

Thank you

Asked By: Mary

||

Answers:

Dictionary needs key and value pairs. That means a dictionary of Dictionaries has key and dictionary(as a value) pairs.

dictofd = {{'name': 'jay', 'age': '78', ... }, {'name': 'kay', 'age': '32', ... },...}

This does not have keys. Therefore, you should use an array of dictionaries or set of dictionaries. If you have to use a dictionary, make some (artificial) keys like the below.

dictofd = { 1: {'name': 'jay', 'age': '78', ... }, 2: {'name': 'kay', 'age': '32', ... }, ...}
Answered By: jjangga

You need to do two things with your list — flatten it out and then convert the strings to int. Flattening it is easy with itertools.chain. After that you can use map() to apply int to each item:

from itertools import chain

mylist = [["14"],["2"],["75"],["15"]]
newest = list(map(int, chain.from_iterable(mylist)))

# newest is => [14, 2, 75, 15]

This will work even if your lists have more than one item like: [["14", "15"],["2"],["75"],["15"]]

Answered By: Mark

Just use a list comprehension

>>> mylist = [["14"],["2"],["75"],["15"]]                                 
>>> [int(item[0]) for item in mylist]
[14, 2, 75, 15]
Answered By: fhgd

Simple solution would be,

newlist = list(map(lambda sl: int(sl[0]), mylist))

You can use for loop as another approach.

newlist = []
for sublist in mylist:
  newlist.append(int(sublist[0]))
Answered By: jjangga

If your sublists might have more than one item, a nice list comprehention solution is:

newlist = [int(str) for sublist in mylist for str in sublist]

You can also refer to this article for more explanation and examples.

Answered By: YAYAdest

If you can use numpy:

import numpy as np
np.ndarray.flatten(np.array([["14"],["2"],["75"],["15"]])).astype(int).tolist()
# Out[6]: [14, 2, 75, 15]
Answered By: oppressionslayer

This function works whatever the size of the lists:

from functools import reduce

my_list = [[1, 2], [10, 5], [-4, 5]]

reduce(lambda a, b: a + b, my_list)

Output: [1, 2, 10, 5, -4, 5]

Answered By: user19843957

To achive this we have multiple ways to do,few examples are:

Using List Comprehension:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = [element for sublist in list_of_lists for element in sublist]
print(flattened_list)

Using itertools.chain:

import itertools

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = list(itertools.chain(*list_of_lists))
print(flattened_list)

Using sum() with List Comprehension:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = sum(list_of_lists, [])
print(flattened_list)

All these methods will produce the same output:

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Answered By: Sachin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.