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

Home Posts Topics Members FAQ

Combining two lists [python union operators]

2 New Member
Hi!

I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly simple. . . but not simple for me.

I have two lists. I end up creating the two lists, with the end result being that each gets created at a different indent level:
Expand|Select|Wrap|Line Numbers
  1.   mylist = entry.objects.filter(title__contains=searchterms1) | entry.objects.filter(keywords__contains=searchterms1).order_by('-dtime')
  2.  
  3.   if (andor2=="1") & (NOT2=="0") :
  4.       mylist3 = mylist.filter(title__contains=searchterms3) & mylist.filter(keywords__contains=searchterms3).order_by('-dtime')
  5.       mylistWHAT=mylist3
  6.  
  7. # here is the big problem and it is a Pythonic problem
  8. #  I need to somehow union the two lists mylistWHAT and mylistWHOM, but they are at different indents.  
  9.  
  10.   mylist=(mylistWHOM  | mylistWHAT)
  11.  
So my problem might be solved by defining both lists as global (which I keep getting syntax errors for - so I don't know how to do this), but it may be that there is some other way to get this last statement to work. I've tried various indents, but this is the only one that the interpreter likes - but one of the lists is then empty.

So how can I declare them global and how can I combine them?

Thanks so much for taking any time to look at this!

-Nat
Jul 7 '07 #1
5 44432
bartonc
6,596 Recognized Expert Expert
Hi!

I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly simple. . . but not simple for me.

I have two lists. I end up creating the two lists, with the end result being that each gets created at a different indent level:
Expand|Select|Wrap|Line Numbers
  1.   mylist = entry.objects.filter(title__contains=searchterms1) | entry.objects.filter(keywords__contains=searchterms1).order_by('-dtime')
  2.  
  3.   if (andor2=="1") & (NOT2=="0") :
  4.       mylist3 = mylist.filter(title__contains=searchterms3) & mylist.filter(keywords__contains=searchterms3).order_by('-dtime')
  5.       mylistWHAT=mylist3
  6.  
  7. # here is the big problem and it is a Pythonic problem
  8. #  I need to somehow union the two lists mylistWHAT and mylistWHOM, but they are at different indents.  
  9.  
  10.   mylist=(mylistWHOM  | mylistWHAT)
  11.  
So my problem might be solved by defining both lists as global (which I keep getting syntax errors for - so I don't know how to do this), but it may be that there is some other way to get this last statement to work. I've tried various indents, but this is the only one that the interpreter likes - but one of the lists is then empty.

So how can I declare them global and how can I combine them?

Thanks so much for taking any time to look at this!

-Nat
Is this what you are trying to do:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3]
  2. >>> list2 = [4, 5, 6]
  3. >>> list1 + list2
  4. [1, 2, 3, 4, 5, 6]
  5. >>> 
?
Jul 7 '07 #2
bartonc
6,596 Recognized Expert Expert
Is this what you are trying to do:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3]
  2. >>> list2 = [4, 5, 6]
  3. >>> list1 + list2
  4. [1, 2, 3, 4, 5, 6]
  5. >>> 
?
Or, perhaps:
Expand|Select|Wrap|Line Numbers
  1. >>>[list1, list2]
  2. [[1, 2, 3], [4, 5, 6]]
  3. >>> 
or
Expand|Select|Wrap|Line Numbers
  1. >>> zip(list1, list2)
  2. [(1, 4), (2, 5), (3, 6)]
  3. >>> 
The indentation doesn't matter. They are in the same scope (the function/method scope) so they are both known to the assignment that you are using. The big thing is "what default value to use when the IF is not taken".
Jul 7 '07 #3
bartonc
6,596 Recognized Expert Expert
Or, perhaps:
Expand|Select|Wrap|Line Numbers
  1. >>>[list1, list2]
  2. [[1, 2, 3], [4, 5, 6]]
  3. >>> 
or
Expand|Select|Wrap|Line Numbers
  1. >>> zip(list1, list2)
  2. [(1, 4), (2, 5), (3, 6)]
  3. >>> 
The indentation doesn't matter. They are in the same scope (the function/method scope) so they are both known to the assignment that you are using. The big thing is "what default value to use when the IF is not taken".
Depending on your version of python (you may need to import the set module in older versions), you can create a true union like this:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3, 2]
  2. >>> list2 = [4, 5, 6, 4]
  3. >>> set.union(set(list1), set(list2))
  4. set([1, 2, 3, 4, 5, 6])
  5. >>> 
Jul 7 '07 #4
TokiDoki
2 New Member
Depending on your version of python (you may need to import the set module in older versions), you can create a true union like this:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3, 2]
  2. >>> list2 = [4, 5, 6, 4]
  3. >>> set.union(set(list1), set(list2))
  4. set([1, 2, 3, 4, 5, 6])
  5. >>> 

Thanks heaps! Your info really helps. I now know that my indentation is not the issue, and I have a nice way to set unions!

Have a super weekend!

-Warren
Jul 7 '07 #5
bartonc
6,596 Recognized Expert Expert
Thanks heaps! Your info really helps. I now know that my indentation is not the issue, and I have a nice way to set unions!

Have a super weekend!

-Warren
It's my pleasure. Any time, really.

You have a great weekend, as well (I'll be toiling away),
Barton
Jul 7 '07 #6

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.) ...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
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...
2
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
5
by: Andy Kent | last post by:
What's the syntax for combining INTO and UNION clauses? What I want to do is: SELECT blah INTO newtable FROM oldtable1 WHERE <conditions> UNION SELECT blah
14
by: AliceB.Toklas | last post by:
In my Absolute Beginner's Guide to C book by Greg Perry where it is instruction how relational operators work it gives the following example: int i = 5; so the following statement is true: ...
0
by: dalebryan1 | last post by:
I am working on a task to display a wireless network nodes using Google Earth (GE) with KML network links. I am using a simple python webserver (see code below) to serve up the python scripts as...
4
by: antar2 | last post by:
Is there a way to convert list_of_listsA to list_of_listsB, where one list in listof lists A is one element of listB? list_of_listsA: , , , , ]
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,...
0
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...
0
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...
0
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
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.