-6

I need to pull an entire list out of an array. Essentially what I need my code to print the following.

a = [1, 2, 3]
b = [(1, 2, 3)]

if a != b
   print "a does not equal b"
else:
   print "a and b are the same!"

>>> a and b are the same!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Devin Liner
  • 419
  • 1
  • 5
  • 11
  • 3
    Have you tried to do anything so far? – Imtiaz Raqib Dec 11 '16 at 05:58
  • How deeply nested can `a` and `b` be? – Akavall Dec 11 '16 at 06:00
  • which is a array and what do you mean pull? – 宏杰李 Dec 11 '16 at 06:00
  • Are you trying to test if both list are the same element-wise? – Christian Dean Dec 11 '16 at 06:01
  • tried converting a into a tuple and comparing the two. tried to turn b into an array and compare the two. tried turning b into a tuple and comparing the two. I think i need to write a for loop that says if element 1 of a and b are the same and if element 2 of a and b are the same and if element 3 of a and b are the same then print that message but idk how to do that yet – Devin Liner Dec 11 '16 at 06:09

3 Answers3

1

Just access the inner tuple and convert to list

a=[1,2,3]
b=[(1,2,3)]

bl = list(b[0])

print(a == bl) # True
datawrestler
  • 1,527
  • 15
  • 17
  • 2
    This is a decent answer for *one* interpretation of a poorly written question. I don't see much point in responding to questions which are so unclear – John Coleman Dec 11 '16 at 06:04
  • i'm still new to python. still unclear about the differences between lists, arrays, and tuples. if a tuple is defined as lists inside of lists with the syntax ((),(),...) and an array is defined to have the syntax [], then i thought b was a list inside of an array. – Devin Liner Dec 11 '16 at 06:16
  • @DevinLiner No. `b` is a [tuple](https://docs.python.org/3.5/tutorial/datastructures.html#tuples-and-sequences) inside of a [list](https://docs.python.org/3.5/tutorial/datastructures.html#more-on-lists). – Christian Dean Dec 11 '16 at 06:17
  • @DevinLiner here is a pretty good discussion on the differences between lists and tuples http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples – datawrestler Dec 11 '16 at 06:21
  • leaf gave me the answer – Devin Liner Dec 11 '16 at 06:25
  • see the answer i provided. – Devin Liner Dec 11 '16 at 06:30
0

Converting it:

def con(st):
    res = []
    for x in st:
        res.append(x)
    return res

So the full code would be:

a = [1, 2, 3]
b = [(1, 2, 3)]
def con(st):
    res = []
    for x in st:
        res.append(x)
    return res
c = con(b[0])
if a != c:
   print "a does not equal b"
else:
   print "a and b are the same!"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ANVGJSENGRDG
  • 153
  • 1
  • 8
-1

The user leaf gave me the answer.

a = [1, 2, 3]
b = [(1, 2, 3)]

if tuple(b[0]) != a:
   print "a does not equal b"
else:
   print "a and b are the same!"

Result >>> a and b are the same!

Community
  • 1
  • 1
Devin Liner
  • 419
  • 1
  • 5
  • 11