473,385 Members | 2,004 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,385 software developers and data experts.

Getting the difference between two lists

440 256MB
Hi,

I have two lists,i have to get the remaining list elements other than common elements.

I/P :

l1 = [1,2,3,4]
l2=[2,3]

O/P
From l1 & l2.I have to get the uncommon elements
[1,4]

Thanks
PSB
May 20 '07 #1
3 21010
bartonc
6,596 Expert 4TB
Hi,

I have two lists,i have to get the remaining list elements other than common elements.

I/P :

l1 = [1,2,3,4]
l2=[2,3]

O/P
From l1 & l2.I have to get the uncommon elements
[1,4]

Thanks
PSB
With sets you can do union, intersection, ect. Or:
>>> l1 = [1, 2, 3, 4]
>>> l2 = [2, 3]
>>> list(set(l1) - set(l2))
[1, 4]
>>>
May 20 '07 #2
ghostdog74
511 Expert 256MB
you should be able to understand how to use sets by now, since barton has showed you how here
May 20 '07 #3
bvdet
2,851 Expert Mod 2GB
If you do not want to use sets, you could do this:
Expand|Select|Wrap|Line Numbers
  1. >>> lst1 = [1,2,3,4,5,6,7,8,9,0,11,12,13]
  2. >>> lst2 = [5,6,7,12,45,67,89,99]
  3. >>> [i for i in lst1+lst2 if i not in lst1 or i not in lst2]
  4. [1, 2, 3, 4, 8, 9, 0, 11, 13, 45, 67, 89, 99]
  5. >>> 
May 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mickel Grönroos | last post by:
Hi! Are there any standard list methods for getting the intersection and difference of two lists? (The union is easy ("list1.extend(list2)"), unless you want it to contain unique values.) ...
42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
1
by: scott289 | last post by:
Getting the lists of users in a given Domain Group? For example, our Network people have created a group called MyAppUsers, and have added certain Users to this group. I need to get a list of...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
5
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4. Let's say I have two scalars, $list1 and $list2. Both are comma separated lists of ordered, unsigned, unique integers. An example would be $list1 = "2,26,345";...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
7
by: Shafik | last post by:
Hello folks, I am an experienced programmer, but very new to python (2 days). I wanted to ask: what exactly is the difference between a tuple and a list? I'm sure there are some, but I can't...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
by: narutocanada | last post by:
hi what is the difference between the two kinds of brackets? I tried a few examples but I can't make out any real difference: lst = print lst print lst print lst lst = (10, 20, 30) print...
0
by: Syed Khaleel Ahamed | last post by:
Dear Sir, I want to get the difference of date/time captured in mysql and present date/time in jsp. I am using the following code to get, but its getting difference only in hrs, i used...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.