1

I have a maybe stupid problem, but can't figure out how to solve it. So basically what I'm trying to do is to:

  • Create a list
  • Add items to the list (stored in a second list)
  • Transform the list into a tuple

So I thought I would do something like this:

anotherList = [2,5,8]
myTuple = tuple([1,2,3].extend(anotherList))

This does not work as it throws the error:

TypeError: 'NoneType' object is not iterable

This makes sense, as the result of the extend operation is not a list. The problem is that I wasn't able to make it in a one line code, but only in a 4 lines code, like that:

anotherList = [2, 5, 8]
myList = [1, 2, 3]
myList.extend(anotherList)
myTuple = tuple(myList)

Is there a way to simplify this code? It looks unnecessarily long!

toti08
  • 2,448
  • 5
  • 24
  • 36
  • `list.extend()` returns `None`, always because the list is updated *in place*. Do not use it on a list literal, that's never going to result in something you want. Did you want to concatenate perhaps? Then use `[...] + anotherlist`. – Martijn Pieters Sep 28 '18 at 10:09
  • @MartijnPieters, yes exactly! I completely missed the `concatenate`, that's it! – toti08 Sep 28 '18 at 10:10
  • @U9-Forward if concatenation would have come to my mind before I certainly would have not asked for this, my problem is that I couldn't figure out what to use in this case... – toti08 Sep 28 '18 at 10:15
  • @toti08 Well, it's marked as duplicate, so what? – U13-Forward Sep 28 '18 at 10:34

3 Answers3

3

You can add lists:

result = tuple([1,2,3] + [4,5,6])

So that'll be something like this:

anotherList = [2, 5, 8]
myList = [1, 2, 3]

myTuple = tuple(myList + anotherList)
ForceBru
  • 43,482
  • 10
  • 63
  • 98
3

You don't want to use list.extend(), period. You are concatenating a list display (literal syntax) with another list instead, use +:

myTuple = tuple([1, 2, 3] + anotherList)

or you could convert just anotherList and prefix a tuple:

myTuple = (1, 2, 3) + tuple(anotherList)

Here the Python compiler can optimise and store the (1, 2, 3) as a constant with your code object. That initial tuple is created just once and reused across all executions.

list.extend() is intended to update an existing list object in-place via a reference to that list, but using it on a list literal means the resulting extended list has no remaining references and is discarded again.

In Python 3.5 and up you could also use the new iterable unpacking syntax:

myTuple = (1, 2, 3, *anotherlist)

Note that no tuple() call is needed there, and any iterable type (not just a list) is supported. Python 2.7 is on it's last days, you do need to start planning to move away from it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

Well, it's unclear tho, but...

Try this:

l=[2,5,8]
print(tuple([1,2,3]+l))

You can concatenate by +

Alternatively can do * (unpacking) in python 3:

print((*l, 1,2,3))

Or can do chain from itertools:

import itertools
print(tuple(itertools.chain(l, [1,2,3])))

Or merge from heapq:

from heapq import merge
print(tuple(merge(l,[1,2,3])))

Or add form operator:

import operator
print(tuple(operator.add(l, [1,2,3])))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114