1

Im trying to append to a list. Im basically slicing up the original and rearranging it. The method im using is as follows... btw im returning a NoneType.

deck = [1,2,3,8,4,5,9,6,7]
def a(deck):
    d1, d2, d3 = (deck[6 + 1:] , deck[3: 6 + 1]
                  , deck[:3])
    deck.append((d1) + (d2) + (d3))

im getting :

[1, 2, 3, 8, 4, 5, 9, 6, 7, [6, 7, 8, 4, 5, 9, 1, 2, 3]]

how can i delete the extra '[ ]' and the the original numbers?

thanks.

  • 4
    Possible duplicate of [Python - append vs. extend](http://stackoverflow.com/questions/252703/python-append-vs-extend) – Peter Wood Oct 31 '15 at 23:55
  • 3
    [**`list.extend`**](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists) – Peter Wood Oct 31 '15 at 23:55
  • 2
    Maybe you want `deck = d1 + d2 + d3`, or `deck[:] = d1 + d2 + d3`, if `deck` is to be used outside the scope of the function. – Peter Wood Oct 31 '15 at 23:57

2 Answers2

0

Easy:

deck = [1,2,3,8,4,5,9,6,7]
def a(deck):
   d1, d2, d3 = (deck[6 + 1:] , deck[3: 6 + 1]
              , deck[:3])
   finallist= deck+ d1+d2+d3
   print (finallist)


a(deck)
>>> [1, 2, 3, 8, 4, 5, 9, 6, 7, 6, 7, 8, 4, 5, 9, 1, 2, 3]

The + operator can do this easily. Isn't Python great? If you want to delete the 'original numbers' you can take out the deck in the finallist variable, like this:

Easy:

deck = [1,2,3,8,4,5,9,6,7]
def a(deck):
   d1, d2, d3 = (deck[6 + 1:] , deck[3: 6 + 1]
              , deck[:3])
   finallist= d1+d2+d3
   print (finallist)
dyao
  • 983
  • 3
  • 12
  • 25
0

Isn't this what you want?

deck = [1,2,3,8,4,5,9,6,7]
def a(deck):
    return deck[6 + 1:] + deck[3: 6 + 1] + deck[:3]

print a(deck)

If you want to modify/change deck then instead of using print just use:

deck = a(deck)

Results:

[6, 7, 8, 4, 5, 9, 1, 2, 3]

Andrés Pérez-Albela H.
  • 4,003
  • 1
  • 18
  • 29