hi all
i hav a list like
list =
using dict() i hav to map like....
{0:344}{1:345}{2,346} and so on
plzzzz help me:(
I think you're confused about the dict notation. It's
d = {key:value, key:value, ...}
Since you already have alternating keys and values in your list, you could do something like:
d = {}
for i in xrange(len(my_list)/2):
key = my_list[i*2]
value = my_list[i*2 + 1]
d[key] = value
if you want to use integers in your dict, instead of the strings in your list, simply do for example:
...
key = int(my_list[i*2])
...
I know there are clever (and probably much more efficient) ways of performing operations on entire lists, but this is simple and readable.
Of course, I have no idea why you'd want or need a dict indexed by successive integers, that's what a list gets you for free! But it's your problem-domain, so good luck and happy coding!
A classical method using list comprehensions
dict([(list[i], list[i+1]) for i in range(0, len(list), 2)])
function dict takes a list of tuples for arguments and uses the first term of the tuple as key and the second as value
If you need more explanations, don't hesitate to ask.
thanks for a reply raptr_dflo
i hav two lists like
list1=
list2 =
i need a output like
{512:344}{513:345}and so on
is it possible to do ??
Can I request the purpose of doing this? We are not here to do your home works one by one.
rssk,
If you understand what I presented previously, then it should be a simple matter to create one loop and access the correct element from each list. jice's input is excellent too, as it removes the division and multiplication.
If you don't understand basic loop flow-control, you should consider getting an introductory programming book for Python (or your choice of other programming language). If you have specific questions other than "tell me how to do this", please feel free to ask.
If it is easier to understand, reduce the lists first and then create the dictionary.
list_1= ['0 ', '512 ', '1 ', '513 ', '2 ', '514 ', '3 ', '515 ', '4 ', '516 ', '5 ', '517 ', '6 ', '518 ', '7 ', '519 ', '8 ', '520 ', '9 ', '521 ', '10 ', '522 ', '11 ', '523 ', '12 ', '524 ', '13 ', '525 ', '14 ', '526 ', '15 ', '527 ', '16 ', '528 ', '17 ', '529 ', '168 ', '530 ', '169 ', '531 ', '170 ', '532 ', '171 ', '596 ', '172 ', '597 ', '173 ', '598 ', '174 ', '599 ', '175 ', '600 ', '176 ', '601 ', '177 ', '602 ', '178 ', '603 ', '179 ', '604 ', '180 ', '605 ', '181 ', '606 ', '182 ', '607 ', '183 ', '608 ', '184 ', '609 ', '185 ', '610 ']
list_2 = ['0', '344', '1', '345', '2', '346', '3', '347', '4', '348', '5', '349', '6', '350', '7', '351', '8', '352', '9', '353', '10', '354', '11', '355', '12', '356', '13', '357', '14', '358', '15', '359', '16', '360', '17', '361', '18', '512', '19', '513', '20', '514', '168', '515', '169', '516', '170', '517', '171', '518', '172', '519', '173', '520', '174', '521', '175', '522', '176', '523', '177', '524', '178', '525', '179', '526', '180', '527', '181', '528', '182', '529']
print [[list_1[y], list_2[y]] for y in range(1, len(list_1), 2)]
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.