Use indexes
You're slicing the list, which return lists. Here, you should use indexes instead:
firstDigit = x[0]
lastDigit = x[-1]
Why is slicing wrong for you:
When you do x[0:1], you're taking the list of items from the beginning of the list to the first interval.
item0, item1, item2, item3
^ interval 0
^ interval 1
^ interval 2
^ interval 3
Doing x[0:2], for example, would return items 0 and 1.
Use indexes
You're slicing the list, which return lists. Here, you should use indexes instead:
firstDigit = x[0]
lastDigit = x[-1]
Why is slicing wrong for you:
When you do x[0:1], you're taking the list of items from the beginning of the list to the first interval.
item0, item1, item2, item3
^ interval 0
^ interval 1
^ interval 2
^ interval 3
Doing x[0:2], for example, would return items 0 and 1.
It all boils down to this:
def addFirstAndLast(x):
return x[0] + x[-1]
In Python, a negative list index means: start indexing from the right of the list in direction to the left, where the first position from right-to-left is -1, the second position is -2 and the last position is -len(lst).
This is probably the most clear approach:
nums = [[19],[5],[23],[6],[10]]
ints = [i[0] for i in nums]
There are other instructive ways to do it as well.
Unpacking from the item works (which makes clear that there's only one element in each item, and will give an unpacking error if there is more than one):
unpacked_ints = [i for i, in nums]
Or can pop from each sublist (the last item, which removes it from the sublist, it will no longer be there if you keep around a reference to the sublists):
popped_ints = [i.pop() for i in nums]
or functionally (operator.itemgetter returns a partial function like lambda x: x[0]):
import operator
ints2 = list(map(operator.itemgetter(0), nums))
in Python2, no need for list to materialize:
import operator
ints3 = map(operator.itemgetter(0), nums)
EDIT in response to comment:
Now your comment is that you're dealing with a big iterable, probably don't want to materialize the whole thing in memory. That still doesn't change how I'd recommend you deal with it. I get the sense you'd like to be able to do this:
for i in nums:
my_process(i)
So I would recommend you do this:
for i in nums:
my_process(i[0])
Another option would be to flatten the list.
nums = [[19],[5],[23],[6],[10]]
nums = [num for numlist in nums for num in numlist]
Or, but only if you want to irritate anybody reading your code:
n = [[19],[5],[23],[6],[10]]
n = [n for n in n for n in n]
;)
str has a join method, where it takes a list as an argument and joins each element by given string.
For example you can create a comma separated line as such:
the_list = ["foo", "bar"]
print(",".join(the_list))
result:
foo,bar
Remember, all elements of the list must be strings. So you should map it first:
the_list = [1, 2, 3, 4]
print(int("".join(map(str, the_list))))
result:
1234
what you could do for this is use list comprehension to turn your list of values into a list of strings. Then you could use the .join method to join the list of strings together. For example:
string_list = [str(num) for num in list]
integer = "".join(string_list)
An example with a list:
list = [3, 4, 5]
string_list = [str(num) for num in list]
integer = "".join(string_list)
print(integer)
Output:
345
The .join() method allows you do combine string list values together with your desired character being specified in the "" quotation marks. List comprehension is a shortcut to make a for loop inside of a list.