this is kind of a quick question, but just a bit complex:

say i want it to find n in range(5), i would do this:

for n in range(5):
      print n

and it would return 0,1,2,3,4.... but what if instead of returning i want it to put it in a list, say list x, such that if i want i can later say

>>>x
[0,1,2,3,4]

is that possible?

my apologies, once again i've found the answer before anyone could reply

x = []
for n in range(5):
    x.append(n)

another quick list question then:

say you have list a = [2,4,6,8,10]
and list b = [2,6,10,12]

is there any way to make a list c show all the ones they have in common? so list c = [2,6,10] ?

i would think its as easy as a & b (as it shows in lesson 5 tutorial), but apparently the minus, plus, &, etc. symbols don't support lists as operands

i've kind of found answer, to convert them to sets (which actually works well because i need to remove duplicates) but if i do that, the numbers go out of order, is it possible to put them back in order from smallest to largest?

ah...nevermind i've figured out how to do it...

my apologies, once again i've found the answer before anyone could reply

x = []
for n in range(5):
    x.append(n)

You're taking the long way here. Look:

>>> x = range(5)
>>> x
[0, 1, 2, 3, 4]
>>>

say you have list a = [2,4,6,8,10]
and list b = [2,6,10,12]

is there any way to make a list c show all the ones they have in common? so list c = [2,6,10] ?

Use sets and then convert back to list and sort() to get it ordered.

>>> setA = set([2,4,6,8,10])
>>> setB = set([2,6,10,12])
>>> rtn = list(setA.intersection(setB))
>>> rtn.sort()
>>> rtn
[2, 6, 10]
>>>

HTH

yeah, that's basically how i did using sets... really i apologize all the information i needed was in the turorial, it just took me some time to get it right...

a = set(a)
b = set(b)
c = b&a

yeah, i know for the [0,1,2,3,4] situation making a list is the long way to take, in reality after the "n in range" there was some other stuff before appending to x....

basically i was making a very inefficient prime number finder:

print ("Prime Number Finder")
x = input("What's the upper range?: ")
if x >= 2:
    print ("2")
a = []
b = []
for n in range(x+1):
    for m in range(n):
        if n is not 0:
            if m is not 0:
                 if m is not 1:
                    if n % m is 0:
                        a.append(n)                            
                    else:
                        b.append(n)
a = set(a)
b = set(b)
c = b-a
for h in sorted(set(c)):
    print h

i know there's probably much better ways to do it, but it works well and the method behind how it works is fairly interesting...
actually i suppose instead of making a new list "b" i could have just used the list "range n"...as you showed:

b = range(x)

that would actually be better because 2 would not fall out as an exception, and i wouldn't have to say print(2) as i did above

as a matter of fact, i did change it to b = range(x+1) .

works great, except it then shows 0 and 1 as prime numbers also, so i had to add an if h >= 2 statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.