Jump to content

Python


a_sagittarian

Recommended Posts

how to return output as dictionary

for example, input ila istunte output display avtundi as below. But I want to return output as dictionary. How is it possible. 

Or: I want to substitute y values with x as reference in a different string. How ?

Input:

for (x, y in .....):

print (x,y)

output:

x0 y0

x1 y1

x2 y2


 

Link to comment
Share on other sites

5 minutes ago, Spartan said:

naak qn ardham kaale.

ne output em ostundi...

ela kavali rendu veyi.

Current_output:

0 abc

1 def

2 klm

Desired_ouput: {0:abc, 1:def, 2:klm}

Link to comment
Share on other sites

18 minutes ago, a_sagittarian said:

Current_output:

0 abc

1 def

2 klm

Desired_ouput: {0:abc, 1:def, 2:klm}

ok, create a dummy dictionary and start assigning values.

dic = {}

dic[0] = abc

dic[1] = def and so on .

 

you can loop that

for x, y in ..

    dic[x] = y

print(dic)

 

  • Upvote 1
Link to comment
Share on other sites

43 minutes ago, jajjanaka_jandri said:

ok, create a dummy dictionary and start assigning values.

dic = {}

dic[0] = abc

dic[1] = def and so on .

 

you can loop that

for x, y in ..

    dic[x] = y

print(dic)

 

Tried... either it prints empty dic or nothing 

Link to comment
Share on other sites

2 hours ago, a_sagittarian said:

Worked 

Cool, but the problem with that is, if there is another key of same type the value gets replaced 

Link to comment
Share on other sites

10 minutes ago, jajjanaka_jandri said:

Cool, but the problem with that is, if there is another key of same type the value gets replaced 

Ikkada aa problem ledu

Link to comment
Share on other sites

3 hours ago, a_sagittarian said:

Tried... either it prints empty dic or nothing 

 

2 hours ago, a_sagittarian said:

Worked 

nizam seppu college lo slippulu baga pettevadivi kada 5b8qx1.gif

  • Haha 2
Link to comment
Share on other sites

list1 = [0,1,2,3,4]
list2 = ['Hello', "How", "are", "you", "?"]

mydict = {}
for x, y in zip(list1, list2):
    mydict[x] = y

print mydict

Output
{0: 'Hello', 1: 'How', 2: 'are', 3: 'you', 4: '?'}

I dont know where you are picking x and y from , I assume its a list. This is working for me

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...