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. 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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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
>...
|
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...
|
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
...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |