364,111 Members | 2011 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Combining two lists [python union operators]

TokiDoki
P: 2
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
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 5,734
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
Expert 5K+
P: 5,734
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
Expert 5K+
P: 5,734
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
P: 2
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
Expert 5K+
P: 5,734
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

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python python union union python