just_in
just_in

Reputation: 447

appending elements to dictionary in python

dictionary = {}

tabl = [('maint_id', 1003L), ('type', 'DB'), ('number', '102')]
for i in tabl:
     dictionary{i[0]:i[1]}


print dictionary

It is giving me only the last key value pair in my dictionary. it is overwritting all the elements. what is wrong with this code.. any help

Upvotes: 1

Views: 115

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336478

It's much easier:

dictionary = dict(tabl)

If order matters:

from collections import OrderedDict
dictionary = OrderedDict(tabl)

Upvotes: 6

isedev
isedev

Reputation: 19641

You want:

for i in tabl:
     dictionary[i[0]] = i[1]

Upvotes: 1

Related Questions