Please help me friends how to get a dictionary (d) from the list(l1).can we do it by dictionary comprehenson??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please help me friends how to get a dictionary (d) from the list(l1).can we do it by dictionary comprehenson???

Input l1=[1,5,22,9,999,55,1000] Output d={1:[1,5,9],2:[22,55],3:999,4:1000}

2nd Jul 2020, 12:59 PM
JMVENKATESAN
JMVENKATESAN - avatar
9 Answers
2nd Jul 2020, 3:39 PM
Ipang
+ 2
d = {i: [n for n in arr if len(str(n)) == i] for i in range(1, max([len(str(n)) for n in arr])+1)}
2nd Jul 2020, 4:21 PM
HonFu
HonFu - avatar
+ 1
Key is number of digits Value is corresponding number from the list. Eg. d[1]=[1,5,9] ,d[2]=[22,55], d [3]=999,d[4]=1000
2nd Jul 2020, 1:22 PM
JMVENKATESAN
JMVENKATESAN - avatar
+ 1
This code is smart. Thank you Ipang !!!
2nd Jul 2020, 4:22 PM
JMVENKATESAN
JMVENKATESAN - avatar
+ 1
Your code is working great. Thank you HonFu .
2nd Jul 2020, 4:30 PM
JMVENKATESAN
JMVENKATESAN - avatar
0
AttributeError. int object has no attribute append
2nd Jul 2020, 1:56 PM
JMVENKATESAN
JMVENKATESAN - avatar
0
Bagon it's throw an error
2nd Jul 2020, 1:57 PM
JMVENKATESAN
JMVENKATESAN - avatar
0
Thanks a lot for your help Bagon
2nd Jul 2020, 2:06 PM
JMVENKATESAN
JMVENKATESAN - avatar
0
No problem JMVENKATESAN 👌
2nd Jul 2020, 7:36 PM
Ipang