2

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

4
  • Do you mean a set of dictionaries? What you've mentioned in the "would need" section is a set, not a dictionary. Commented Dec 1, 2019 at 23:29
  • 3
    What are the keys of dictofd? Commented Dec 1, 2019 at 23:30
  • 1
    A dictionary requires a KEY and a VALUE. The one you show as dictofd is not a valid dictionary Commented Dec 1, 2019 at 23:31
  • Sorry, I realised the error Commented Dec 1, 2019 at 23:34

8 Answers 8

5

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"]]

Sign up to request clarification or add additional context in comments.

Comments

2

Just use a list comprehension

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

1 Comment

Maybe worth pointing out that this will only work if the sublists never have more than one item.
1

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]

Comments

0

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', ... }, ...}

1 Comment

Heads up: the OP edited the question to be completely different after this answer was posted.
0

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]))

Comments

0

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.

Comments

0

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]

Comments

-1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.