473,322 Members | 1,610 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,322 software developers and data experts.

Idiomatic Python to convert list to dict

Hi all,

Simple question really on a best practice. I want to avoid adding
duplicates to a list.

my_list = ['a', 'b', 'c', 'd', 'e']
dup_map = {}
for item in my_list:
dup_map[item] = True

# ... sometime later

for complex_dict in large_list:
if complex_dict["char"] not in dup_map:
my_list.append(complex_dict["char"])
dup_map[complex_dict["char"]] = True

For the first part (generating the duplicate map) is there a more
idiomatic Python method for flipping the list into a dict?

Is there a better way to achieve the overall objective (as hinted at
by the above algorithm)?

Thanks in advance for any tips.

James.
Jul 10 '08 #1
3 1557
James Fassett schrieb:
Hi all,

Simple question really on a best practice. I want to avoid adding
duplicates to a list.

my_list = ['a', 'b', 'c', 'd', 'e']
dup_map = {}
for item in my_list:
dup_map[item] = True

# ... sometime later

for complex_dict in large_list:
if complex_dict["char"] not in dup_map:
my_list.append(complex_dict["char"])
dup_map[complex_dict["char"]] = True

For the first part (generating the duplicate map) is there a more
idiomatic Python method for flipping the list into a dict?

Is there a better way to achieve the overall objective (as hinted at
by the above algorithm)?

Thanks in advance for any tips.
Instead of a dict, use a set. It's immediatly contructable from my_list,
and better suited for the task anyway.

Diez
Jul 10 '08 #2
On Jul 10, 10:06*am, James Fassett <ja...@reggieband.comwrote:
Hi all,

Simple question really on a best practice. I want to avoid adding
duplicates to a list.

my_list = ['a', 'b', 'c', 'd', 'e']
dup_map = {}
for item in my_list:
* * dup_map[item] = True

# ... sometime later

for complex_dict in large_list:
* * if complex_dict["char"] not in dup_map:
* * * * my_list.append(complex_dict["char"])
* * * * dup_map[complex_dict["char"]] = True

For the first part (generating the duplicate map) is there a more
idiomatic Python method for flipping the list into a dict?

Is there a better way to achieve the overall objective (as hinted at
by the above algorithm)?

Thanks in advance for any tips.

James.
The dictionary seems like overkill here because, if I understand
correctly, the only value associated with a key is "True". So in that
case just remove all the code related to the dictionary (and
complex_dict) and you end up with

my_list = ['a', 'b', 'c', 'd', 'e']

# ... sometime later
for char in large_list:
if char not in my_list:
my_list.append(char)
However, as Diez suggests, use a set:

my_list = set(['a', 'b', 'c', 'd', 'e'])
# ... sometime later
for char in large_list:
my_list.add(char) # will not add duplicates
Jul 10 '08 #3
On Jul 10, 6:13*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
my_list = ['a', 'b', 'c', 'd', 'e']
dup_map = {}
for item in my_list:
* * dup_map[item] = True
# ... sometime later
for complex_dict in large_list:
* * if complex_dict["char"] not in dup_map:
* * * * my_list.append(complex_dict["char"])
* * * * dup_map[complex_dict["char"]] = True

Instead of a dict, use a set. It's immediatly contructable from my_list,
and better suited for the task anyway.
Cheers,

I rewrote it similar to:

dup_map = set(['a', 'b', 'c', 'd', 'e'])

# ... sometime later

for complex_dict in large_list:
dup_map.add(complex_dict["char"])

my_list = list(dup_map)

That is a little nicer. Thanks again,
James.
Jul 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
25
by: Byte | last post by:
I know this is probably a stupid question, but I'm learning Python, and am trying to get the if function to work with letters/words. Basicly, I'm trying to write a script that when run, says ...
0
by: rkmr.em | last post by:
the memory usage of a python app keeps growing in a x86 64 linux continuously, whereas in 32 bit linux this is not the case. Python version in both 32 bit and 64 bit linux - 2.6.24.4-64.fc8 Python...
20
by: Mr.SpOOn | last post by:
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.