Can only concatenate list (not "str") to list - list

I have written the following code snippet:
origilist = [6, 252, 13, 10]
decilist = list(xrange(256))
def yielding(decilist, origilist):
half_origi = (len(origilist)/2)
for n in decilist:
yield origilist[:half_origi] + n + origilist[half_origi:]
for item in yielding(decilist, origilist):
print item
when I run the code I get:
yield origilist[:half_origi] + n + origilist[half_origi:]
TypeError: can only concatenate list (not "int") to list
Is there anyway of joining an integer to another list, in a specific index?
Thanks for the answers

You can only concat lists and n is the item in your list, not list. If you want to concat it with lists, you need to use [n], which is basically creating a list of one item.
In general, you can use + also for strings, but every item you want to concat needs to be string then. So if you have two lists and want to add any item to them, you need to convert that item to list.

Try:
yield origilist[:half_origi] + [n] + origilist[half_origi:]
([n] is a list with one element that can be concatenated to other lists).

Related

How do I add the output of a for-loop to a list in Scala?

I'm looking to iterate over a list of tuples and extract the second value from each tuple. I'd then like to add these values to a new list.
Each tuple in my list consists of a string and an int:
y = List((this, 1), (is, 2), (a, 3), (test, 4)
I can successfully iterate over each tuple and extract the int using:
for (x <- y) {
val intValue = x._2
println(intValue)
}
This gives me
1
2
3
4
I'm then looking to add those values to a list. Any help would be appreciated.
There is no need to use for here, a simple map will do:
y.map(_._2)
The map function creates a new List from the old one by calling a function on each element of the List.
The function _._2 is a shorthand for x => x._2. This just returns the second value in the tuple, which is the Int you want to extract.
You can use map function as #Tim explained or just use yield like in:
val list = for ((_,b) <- y) yield b

Calling list objects inside of a list

alphabet = ['a','b','c']
numbers = [3,2,1]
print alphabet(numbers)
How do I use a list inside of another list? I am trying to get the output "cba" but am running into the following error
TypeError: 'list' object is not callable
You have to index the second list, so you're referencing individual integers rather than the list.
For example, print alphabet[numbers[1]] is the equivalent of print alphabet[2].
Remember that lists are indexed starting at 0, so for alphabet = ['a','b','c'], 'a' is at index 0, 'b' is at index 1, and so on.
To do what you want, you are going to need to use a loop to iterate through the numbers list. Something like:
alphabet = ['a','b','c']
numbers = [2,1,0]
for n in numbers:
print alphabet[n]

Python: How to append elements to a list using list iterator

Let's say we have an existing empty list, m. I would like to append some elements (let's say three 7's) to m so that m is now [7,7,7]. How to do this using list iterators?
I have the following solution, which is not complete:
m = []
m.append([7 for i in range(3)])
I get m = [[7,7,7]] but not [7,7,7].
I am not looking for post-processing like making m flat such as:
flat_list = [item for sublist in l for item in sublist], because it is an extra step.
You may want to try extend rather than append.
m = []
m.extend([7 for i in range(3)])
Here is a post that may be helpful

Create a List out of an tuple array

I am scripting in Python2.7.I have an array like k = ((12,12.356,40.365),(458,12.2258,42.69)) and I want to create a list out of the first element of each subset like:
for i in K:
l = k[i][0]
While I have encountered the error "tuple indices must be integers, not tuple".
Would you please let me know how can I come up with a solution?
You are trying to access k tuple item by tuple like k[(12,12.356,40.365)][0], while an item of ordered sequence should be accessed by its position/index (for ex. k[0][0]).
Use list comprehension to get a list out of the first element of each subset:
k = ((12,12.356,40.365),(458,12.2258,42.69))
result = [t[0] for t in k]
print(result)
The outtut:
[12, 458]

Python3 - Change the type of specific list elements

I have a list:
alist =
[('A','1','2','DEF'),('B','100,'11','XYZ'),('C','6','9','ABC')]
and I want to sort the list on 2nd and 3rd element, but before I do so I want to convert the type of these elements from string to integer. How can I do so in most pythonic way?
I know I can read the list, convert elements to integer, add all elements to a new list and finally sort:
newList = []
for i in alist:
a,b,c,d, = i
newList.append((a,int(b),int(c),d))
newList.sort(key=itemgetter(1,2))
but what if each tuple in my list has 100 elements (above list had just 4) and I just want to convert a few them (like in above list - b and c) to integer type?
Bade
There's no need to convert them if you only want to sort on them:
alist.sort(key=lambda x: (int(x[1]), int(x[2])))
...
newList = sorted(alist, key=lambda x: (int(x[1]), int(x[2])))
If I inderstand the question, you really want to sort tuples by key, which needs to be converted to integer.
I would do
sorted_list = sorted(alist, key=lambda tup: int(tup[1]))