473,685 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ 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 44440
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
80972
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.) Here is what I would like to have: list1 = list2 =
226
12567
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
42
2734
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 a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it than just that. Everything I tried with a list worked the same with a tuple. So, what's the difference and why choose one over the other? Jeff
2
3276
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. Javascritp has >>> and >>. >>> is a zero fill bit shift whereas >> is a sign propagating bit shift. My understanding is that the python >> is equivalent to the javascript >> - but python has no equivalent to >>>. Would a >>> 3 in javascript be...
5
33916
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
2558
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: int i == 1
0
966
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 KML output to GE for this. import BaseHTTPServer import CGIHTTPServer class Handler(CGIHTTPServer.CGIHTTPRequestHandler): cgi_directories = httpd = BaseHTTPServer.HTTPServer(('',8000), Handler)
4
2035
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
8570
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
8504
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,...
0
8925
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8758
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
7584
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
6431
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
5790
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
4521
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1924
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.