1

This is the list. I want to extract all numbers from list including numeric values of both keys and values from dictionary.

list1 = [1,2,3,4, [44,55,66, True], False, (34,56,78,89,34), {1,2,3,3,2,1}, {1:34, "key2": [55, 67, 78, 89], 4: (45, 22, 61, 34)}, [56, 'data science'], 'Machine Learning']

def prod_list(lst):
    flat_list = []
# iterate over each element 
for i in lst: 

    # check if element is list or number 
    if type(i) == list or type(i) == tuple or type(i) == set or type(i) == dict: 
        # iterate over each nested list 
        for j in i: 
            # check if element is number 
            if type(j) == int or type(j) == float: 
                flat_list.append(j) 
            elif type(j) == dict:
                for k,v in j.items():
                    if type(v) == int or type(v) == float:
                        flat_list.append(v)

            elif type(j) == list or type(j) == tuple:
                for k in j:
                    if type(k) == int or type(k) == float:
                        flat_list.append(k)
    else: 
        # check if element is number 
        if type(i) == int or type(i) == float: 
            flat_list.append(i) 

# calculate product of list 
return(flat_list)

#I am expecting this list
n=[1,2,3,4,44,55,66,34,56,78,89,34,1,2,3,1,34,55,67,78,89,4,45,22,61,34,56]``your text``

3 Answers3

0

Try:

def is_integer(x):
    return not isinstance(x, bool) and isinstance(x, int)

def get_numbers(o):
    if isinstance(o, dict):
        for k, v in o.items():
            if is_integer(k):
                yield k
            yield from get_numbers(v)
    elif isinstance(o, (list, tuple, set)):
        for v in o:
            if is_integer(v):
                yield v
            else:
                yield from get_numbers(v)
    else:
        if is_integer(o):
            yield o

list1 = [
    1,
    2,
    3,
    4,
    [44, 55, 66, True],
    False,
    (34, 56, 78, 89, 34),
    {1, 2, 3, 3, 2, 1},
    {1: 34, "key2": [55, 67, 78, 89], 4: (45, 22, 61, 34)},
    [56, "data science"],
    "Machine Learning",
]

print(list(get_numbers(list1)))

Prints:

[1, 2, 3, 4, 44, 55, 66, 34, 56, 78, 89, 34, 1, 2, 3, 1, 34, 55, 67, 78, 89, 4, 45, 22, 61, 34, 56]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

Try this code to Extract all numeric values form given list

def flat_list(n):

    l = []
    
    for i in list1:
    
        if type(i)==list or type(i)==tuple or type(i)==set: # check if element is list,tuple and set 
            for j in i: # iterate over each nested list 
                if type(j)==int: # check if element is int
                    l.append(j)
                    
        elif type(i)==dict: # check if element is dict
            
            # iterate over each nested dict 
            for k in i.items():
                if type(k[0])==int:
                    l.append(k[0])
           
                if type(k[1])==tuple or type(k[1])==list:
                    for v in k[1]:
                        if type(v)==int:
                            l.append(v)
                else:
                    if type(k[1])==int:
                        l.append(k[1])
        else:
            if type(i)==int:
                l.append(i)
    return (l)

print(flat_list(list1))
       

Output: [1, 2, 3, 4, 44, 55, 66, 34, 56, 78, 89, 34, 1, 2, 3, 1, 34, 55, 67, 78, 89, 4, 45, 22, 61, 34, 56]
-1
from functools import reduce
def product(lst):
    flatList=[]
    for i in lst:
        if type(i)==list or type(i)==tuple or type(i)==set:
            for j in i:
                if type(j)==int:
                    flatList.append(j)
        elif type(i)==dict:
            for a in i.keys():
                if type(a)==int:
                    flatList.append(a)
            for b in i.values():
                if type(b)==int:
                    flatList.append(b)
                if type(b)==list or type(b)==tuple:
                    for c in b:
                        if type(c)==int:
                            flatList.append(c)
        else:
            if type(i)==int:
                flatList.append(i)
    return flatList
list1 = [1,2,3,4, [44,55,66, True], False, (34,56,78,89,34), {1,2,3,3,2,1}, {1:34, "key2": [55, 67, 78, 89], 4: (45, 22, 61, 34)}, [56, 'data science'], 'Machine Learning']
print(product(list1))

I used this code.

buddemat
  • 4,552
  • 14
  • 29
  • 49
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '23 at 22:16