-3

for ex.

lst = [2,'j','K','o',6,'x',5,'A',3.2]

I want 3 lists in the form

numbers = [2, 3.2, 5, 6],
uppercase = ['A', 'K'],
lowercase = ['j', 'o', 'x']

I wish to change them into a dictionary of the form

{'numbers': [2, 3.2, 5, 6],
 'uppercase': ['A', 'K'],
 'lowercase': ['j', 'o', 'x']

how can I achieve this? any help will be appreciated

1 Answers1

0

This is a solution to the problem:

lst = [2,'j','K','o',6,'x',5,'A',3.2]

# get the numbers
numbers = [x for x in lst if type(x)==int or type(x)==float ]

# get the strings
strings = [x for x in lst if type(x)==str]

# get the uppers
upper = [x for x in strings if x.isupper()==True]

# get the lowers
lower = [x for x in strings if x.islower()==True]

# now put into a dict
d = {
    'numbers':numbers,
    'upper':upper,
    'lower':lower
    }

# this is the answer as a dict
print(d)

# here there are deperately
print(numbers)
print(upper)
print(lower)

result:

{'numbers': [2, 6, 5, 3.2], 'upper': ['K', 'A'], 'lower': ['j', 'o', 'x']}

And the lists:

[2, 6, 5, 3.2]
['K', 'A']
['j', 'o', 'x']
D.L
  • 4,339
  • 5
  • 22
  • 45