473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to create a dictionary using 2 lists?

I've 2 lists say:
Expand|Select|Wrap|Line Numbers
  1. list1 = ['a','b','c']
  2. list2 = [1,2,3]
and i want a dictionary as an output:
Expand|Select|Wrap|Line Numbers
  1. dict1 = {'a':1,'b':2,'c':3}
How to achieve this using Built in Functions?
Oct 7 '10 #1
4 2081
Frinavale
9,735 Expert Mod 8TB
Why not just loop through each list and set the dictionary values?

For example:
(pseudo code)
Expand|Select|Wrap|Line Numbers
  1. Loop through List1 and List2
  2.   For this iteration get the Key from List1
  3.   For this iteration get the Value from List2
  4.   Add an item to the dictionary at the Key with the Value for this iteration
  5. Increment the iteration and go back to the start of the loop
-Frinny
Oct 7 '10 #2
Thanks mate for your reply..

i was looking for some built in function.. i faced an interview today and i was clueless when they asked me to use built in functions of python..
Oct 7 '10 #3
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> dict(zip(['a','b','c'], [1,2,3]))
  2. {'a': 1, 'c': 3, 'b': 2}
  3. >>> 
Oct 7 '10 #4
Thanks mate..
That works :)
Oct 7 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: quadric | last post by:
Hi, I have an application that has an embedded interpreter. This application loads many DLL's and passes a PyObject * to each DLL that was gotten from the following call: PyObject * pmod =...
1
by: Sakharam Phapale | last post by:
Hi All, I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide...
6
by: Shibu | last post by:
Hi, Can anyone tell me how to create a DSN from asp.net.. Thanks Shibu
0
by: Sakharam Phapale | last post by:
I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide me how to...
1
by: mandeep singh | last post by:
hiii , i am a student doing my bachelors degree.ths a group of all technical pepoles. can someone suggest me by giving me the platform how to create games using C++. i haved done banking...
5
by: reon | last post by:
While we can create files using c++... Is there any code relating to create folders in c++..
1
by: brixton | last post by:
Hello, probably a simple problem but I can't find how to solve it so here we go... Instead of using a table I'm using lists and CSS for my layout. How do I set the "width" for my list item...
2
by: bpowah | last post by:
I searched for an hour and don't see a solution to this (i assume somewhat common) problem. I have a very large dictionary of lists: d = {a:, b:, c:} and i want to reverse the associativity of...
10
by: islayer | last post by:
i am trying to write a dictionary using perl, the program would use a primary .pl file, and a text file, designated .dat i have no programing background, and am trying to teach myself perl, and...
0
by: ramilol | last post by:
i wanna create cursor but i have no idea how all i know is that ill use create cursor to do it but the last two paremters need a some 00xff values i have no idea how to use them can't i just draw...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.