473,485 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

List match

Hi there:

I know this probably is a very easy thing to do in python, but i wanted
to compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]

hence the double entry would not show up?

Is there a way to do this??

Stephen

Aug 17 '06 #1
7 1657

[Stephen]
[...] compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]
Use sets:
>>from sets import Set as set # For compatibility with Python 2.3
one = ["apple", "banana", "grape"]
two = ["orange","banana", "pear"]
print list(set(one) | set(two))
['grape', 'apple', 'orange', 'pear', 'banana']

--
Richie Hindle
ri****@entrian.com
Aug 17 '06 #2

OriginalBrownster wrote:
Hi there:

I know this probably is a very easy thing to do in python, but i wanted
to compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]

hence the double entry would not show up?

Is there a way to do this??

Stephen
How about:

list3 = list1 + [item for item in list2 if item not in list1]

print list3:

['apple', 'banana', 'grape', 'orange', 'pear']

Aug 17 '06 #3

OriginalBrownster wrote:
Hi there:

I know this probably is a very easy thing to do in python, but i wanted
to compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]

hence the double entry would not show up?

Is there a way to do this??

Stephen
How about:

list3 = list1 + [item for item in list2 if item not in list1]

print list3:

['apple', 'banana', 'grape', 'orange', 'pear']

Aug 17 '06 #4

"OriginalBrownster" <or***************@gmail.comwrote in message
news:11********************@74g2000cwt.googlegroup s.com...
I know this probably is a very easy thing to do in python, but i wanted
to compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]
Other people have already posted solutions but I'll add a couple of
comments:

1. Avoid calling lists 'list', you will break the list built-in function.

2. You don't specify whether the lists are ordered. If there is no
significance to the order of the items, it may be more appropriate
to use sets throughout. Then the answer is just: set3 = set1 | set2.
Aug 17 '06 #5
Richie Hindle schrieb:
[Stephen]
>>[...] compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]


Use sets:

>>>>from sets import Set as set # For compatibility with Python 2.3
one = ["apple", "banana", "grape"]
two = ["orange","banana", "pear"]
print list(set(one) | set(two))

['grape', 'apple', 'orange', 'pear', 'banana']
Why using Set two times, when you can do it with one call?
>>list(set(one + two))
According to my benchmarks, this was five times faster than calling it
twice and use |.

There are also a few more good approaches uniquifying lists at
http://www.peterbe.com/plog/uniqifiers-benchmark

Regards,
Stargaming

Aug 17 '06 #6

th*********@gmail.com wrote:
OriginalBrownster wrote:
Hi there:

I know this probably is a very easy thing to do in python, but i wanted
to compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]

hence the double entry would not show up?

Is there a way to do this??

Stephen

How about:

list3 = list1 + [item for item in list2 if item not in list1]

print list3:

['apple', 'banana', 'grape', 'orange', 'pear']
It works with the data given, but if list1 contains duplicates...

- Pad

Aug 17 '06 #7

OriginalBrownster wrote:
Hi there:

I know this probably is a very easy thing to do in python, but i wanted
to compare 2 lists and generate a new list that does not copy similar
entries. An example below

list= ["apple", "banana", "grape"]
list2=["orange","banana", "pear"]

now I want to compare these lits and generate a third list after
comparison

list3 would be ["apple", "banana","grape","orange", "pear"]

hence the double entry would not show up?

Is there a way to do this??

Stephen
This solution uses sets; removes all duplicates and is order
preserving.
Remove the outer sorted wrapper if the order need not be preserved.
>>lst= ["apple", "banana", "grape"]
lst2=["orange","banana", "pear"]
lst3= lst+lst2
sorted([x for x in set(lst3)], key = lambda x: lst3.index(x))
['apple', 'banana', 'grape', 'orange', 'pear']
>>>
- Paddy

Aug 17 '06 #8

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

Similar topics

7
2937
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
0
1010
by: Brian van den Broek | last post by:
Mark Devine said unto the world upon 2004-12-16 10:49: > Hi I'm brand new to python and I was wondering if anybody knew of a > easy way to change every character in a list into its lower case >...
1
13994
by: marx | last post by:
I have a bit of a problem and any help would be much appreciated. Problem: I have two dropdown list boxes with same data(all data driven). These are used for two separate entries. For every...
5
5731
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
2
7149
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is...
1
1425
by: Bucco | last post by:
I have taken into advice what was mentioned earlier, but I have run into a snag in which I am getting the wrong output from my class. output: set() The first line of output correctly...
1
11883
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
7
4700
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such...
5
18590
by: Mr.SpOOn | last post by:
Hi, is there any way to search elements in a list using wildcards? I have a list of various elements and I need to search for elements starting with 'no', extract them and put in a new list. I...
3
2722
by: Saser | last post by:
Hi. I have two questions. #1. I'm thinking of creating a web service which contain a list object which contain a number of entrys of the own-defined class Match, which will have the variables:...
0
7085
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6957
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7155
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...
1
6820
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...
1
4856
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...
0
4544
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3053
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
593
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.