Adding List Elements into dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Avatar19
    New Member
    • Apr 2010
    • 43

    Adding List Elements into dictionary

    Code:
    List1 = []
    
    while True:
        A = raw_input()
        if A:
            A = A.split(" ")
            List1.append(A)
        else:
            break
    print List1
    if the input for example = "A 000", B 10"
    print List1 returns
    [[A,000],[B,10]]
    if want to know how to add these elments into a dictionary, let D = {}, and for e.g.
    A is the 1st key, 000 is the value associated with A, etc for the rest of the elements in the List
    Thank you for any help i would really appreciate it!
    Last edited by bvdet; Apr 10 '10, 05:13 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Code:
    >>> list1 = []
    >>> while True:
    ... 	a = raw_input()
    ... 	if a:
    ... 		list1.append(a.split())
    ... 	else:
    ... 		break
    ... 	
    >>> print list1
    [['A', '000'], ['B', '10']]
    >>> dd = {}
    >>> for item in list1:
    ... 	dd[item[0]] = item[1]
    ... 	
    >>> dd
    {'A': '000', 'B': '10'}
    >>>

    Comment

    • Avatar19
      New Member
      • Apr 2010
      • 43

      #3
      Thank you that answered it!!

      Comment

      Working...