473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ 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","banan a", "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 1662

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

list= ["apple", "banana", "grape"]
list2=["orange","banan a", "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","banan a", "pear"]
print list(set(one) | set(two))
['grape', 'apple', 'orange', 'pear', 'banana']

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

OriginalBrownst er 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","banan a", "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

OriginalBrownst er 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","banan a", "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

"OriginalBrowns ter" <or************ ***@gmail.comwr ote in message
news:11******** ************@74 g2000cwt.google groups.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","banan a", "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","banan a", "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","banan a", "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(on e + 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*********@gma il.com wrote:
OriginalBrownst er 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","banan a", "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

OriginalBrownst er 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","banan a", "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","banan a", "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
2958
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
1021
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 > form. > > The list is like so: commands = > > and what I want is: commands = > > Are there any defined method within any known modules to do this in
1
14023
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 entry you cannot choose the same value twice. For example, I cannot choose for entry 1 the same value in both selection boxes (gqCategory1Entry1 and gqCategory2Entry1)
5
5744
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 have a list, as below: sentence = "the color is $red" patterns = pos = sentence.find($)
2
7163
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 a match. If a match is found, I need to move the matched item into another list. I want to compare the objects by value. The items in each list have an ID property (of type object) which I want to convert to a strng in order to be able to do...
1
1428
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 displays the list of file names in the argv directory.
1
11895
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
4708
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 as the following: "Smith said the dog belongs (or did belong) to the secretary, an employee of the company." Then the list contains the following (and this list is MUCH larger in the real situation):
5
18660
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 was thinking about something like: mylist.index('no*') Of course this doesn't work.
3
2732
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: MapID (int) Number of players (int) SkillLevel (enum) ServerIsOn (bool) In a Windows application you can either choose to search for matches or create a match which will be searchable to others. If you choose to create a match, a new instance of...
0
8145
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8095
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8410
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6068
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.