Python += with a list and a tuple [duplicate] - python

This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why can't I add a tuple to a list with the '+' operator in Python?
(3 answers)
Closed 7 years ago.
I saw someone wrote an interesting python line online, but couldn't understand why it works. So we can try the following lines in python interpreter:
s=[1]
s=s+(1,-1)
This will result in an error "TypeError: can only concatenate list (not "tuple") to list". But if done in another way:
s=[1]
s+=(1,-1)
will result in s = [1,1,-1]
So I used to thought x=x+y is equivalent to x+=y, can someone tell me how they are different and why the second way works? Thanks in advance.

Instead of += use list.extend:
s = [1]
s.extend((1,-1))

Related

Python List Reverse Function [duplicate]

This question already has answers here:
How can I get a reversed copy of a list (avoid a separate statement when chaining a method after .reverse)?
(11 answers)
Closed 2 years ago.
Python Novice. Need to create a list which is a reverse of the previous list. Ran the follow code:
list_10 = [1,10,20,4,40,14]
list_11 = list_10.reverse()
print (list_11)
I get an output that says "None"
Any suggestions?
Because reverse() reverses the list in place, it returns none.
Try this.
list_10 = [1,10,20,4,40,14]
list_10.reverse()
list_11 = list_10

Python List as List Index [duplicate]

This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]

I was iterating over a List and print each element, why the code throws error? [duplicate]

This question already has answers here:
TypeError: 'int' object is not callable
(10 answers)
Closed 5 years ago.
I was iterating over a List and print each element, the code is very simply, but the program throws error.
You have redefined print somewhere else in your code. You can test this by simply running:
print
It will likely give you an integer. Try resetting your kernel.

* before iterable inside a print() in Python [duplicate]

This question already has answers here:
What does asterisk * mean in Python? [duplicate]
(5 answers)
Closed 5 years ago.
I have a list of names and I want to print each element of the list in a different line without a for loop. So, after some research I found this example: print(*names, sep='\n'), witch results in exactly what I want. But what does this * character before the list name means?
The * is used to unpack argument lists when calling a function. In this case it unpacks your list of names.

TypeError: list object not found in python [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
in my code in python
for i in array_config(1,len_config):
print array_config[2]
shows
TypeError: 'list' object is not callable
(array_config is an array of configuration file)
(len_configs value is 7)
how to remove that error?
You're calling array_config like a function in the first line. It feels like you meant to write this:
for i in array_config:
print i

Categories

Resources