Webair
Webair

Reputation: 105

create dictionary from list

I am trying to make a dictionary with two lists using map function. But something doesn't seem right.

I know there is already zip and dict to do this job, but I am wondering why this code went wrong and where.

country = ['India', 'Pakistan', 'Nepal', 'Bhutan', 'China', 'Bangladesh']
capital = ['New Delhi', 'Islamabad','Kathmandu', 'Thimphu', 'Beijing', 
'Dhaka']

country_capital={}

def mydict(x,y):
  country_capital[x]=y
  return country_capital
national_info=map(mydict,country,capital)
print (list(national_info))

why it prints as below:

[{'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}]

I wanted like this:

[{'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}]

Upvotes: 1

Views: 664

Answers (6)

user9158931
user9158931

Reputation:

You can try something like this:

print({i[0]:i[1] for i in zip(country,capital)})

Upvotes: 0

AndreyF
AndreyF

Reputation: 1838

People here suggested much cleaner way to solve your problem (a way you are aware of as mentioned in your question).

Regarding your question:

why it prints as below:

[{'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}, {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 
'Beijing', 'Bangladesh': 'Dhaka'}]

The map method applies your function on each element of your lists and collect the return values to a new list.

Your function returns the global variable country_capital to which you add elements. Therefore the output of your map (national_info) is a list with n pointers to country_capital.

If you'll print country_capital instead of national_info you'll get the output you want.

Upvotes: 2

Saurav Shanu
Saurav Shanu

Reputation: 171

All the given answers will get you the results you need. However, you can never get the results you need with the code you wrote. The resultant output list after applying map has always the number of elements equal to the number of elements in the given list. As you are changing the same global dict, so returning it each time will send the reference of the same dictionary.

Upvotes: 0

Rakesh
Rakesh

Reputation: 82765

country = ['India', 'Pakistan', 'Nepal', 'Bhutan', 'China', 'Bangladesh']
capital = ['New Delhi', 'Islamabad','Kathmandu', 'Thimphu', 'Beijing', 'Dhaka']


d = dict(zip(country, capital )) #{i:v for i, v in zip(country, capital )}
print d

Result:

{'Pakistan': 'Islamabad', 'Bangladesh': 'Dhaka', 'Bhutan': 'Thimphu', 'Nepal': 'Kathmandu', 'India': 'New Delhi', 'China': 'Beijing'}

Upvotes: 0

RoadRunner
RoadRunner

Reputation: 26315

You can map() tuples of (country, capital), then wrap dict to get the final dictionary:

>>> country = ['India', 'Pakistan', 'Nepal', 'Bhutan', 'China', 'Bangladesh']
>>> capital = ['New Delhi', 'Islamabad','Kathmandu', 'Thimphu', 'Beijing', 'Dhaka']
>>> dict(map(lambda x, y: (x, y), country, capital))
  {'India': 'New Delhi', 'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'Bhutan': 'Thimphu', 'China': 'Beijing', 'Bangladesh': 'Dhaka'}

Upvotes: 0

timgeb
timgeb

Reputation: 78650

dict accepts an iterable of two-element iterables, so all you need to issue is

country_capital = dict(zip(country, capital))

(For some reason, your expected result wraps this dictionary in a one-element list. I don't see any reason to do this.)

Upvotes: 2

Related Questions