Python challenge No3

Hello every body !

My challenge for today is how many ways can you create this dict with?

for me I have 7 different ways how about you?

2 Likes

Hi @mr.sha3ban

I probably have like 5 but I guess even they will have some form of repetition. :sheep: ish :smiling_face: :slightly_frowning_face:

1 Like

@Rucha
Show me the code
:thinking:

.
.

hey @mr.sha3ban

now I guess I know only one method… So this will be like the same stuff in different packaging!

    test1 = {1: "a", 2: "b", 3: "c"}
    test2 = dict([(1, "a"), (2, "b"), (3, "c")])
    test3 = dict(zip([1, 2, 3], ["a", "b", "c"]))
    
    keys = [1, 2, 3]
    values = ["a", "b", "c"]
    if len(keys) == len(values):
        test4 = {k:v for (k,v) in zip(keys, values)}

    test5 = {}
    for k,v in zip(keys, values):
           test5[k] = v 

Okay fine, they are all the same! … Again :sheep:ish :grimacing:

2 Likes

can you please list the seven ways?

i can only think of two right now

1 Like
d1= {1:'a',2:'b',3:'c'}


d2= {x:y for y,x in dict(a=1,b=2,c=3).items()}


d3= dict([(1,'a'),(2,'b'),(3,'c')])


keys=1,2,3
values='a','b','c'
d4= dict(zip(keys,values))



lst=['a','b','c']
d5={x:y for(x,y)in enumerate(lst,start=1) }


s="abc"
d6={lst.index(i)+1:i for i in lst}



d7=dict((i, chr(i+96)) for i in range(1,4))







print(*[d1,d2,d3,d4,d5,d6,d7],sep='\n')

4 Likes

Okay that’s good, i didn’t think of that

1 Like