This question already has answers here:
Python list confusion [duplicate]
(3 answers)
Closed 8 years ago.
I want to change only one element in the 2d list. I can change an element in list1 using list1[0][2] = "x" but when i do the same for list2 more than one element is changed.
list1 = []
for i in range(0,5):
list1.append(['O']*5)
list2 = [['o','o','o','o','o']]*5
Because this is 5 copies of the same list
list2 = [['o','o','o','o','o']]*5
Getting a good understanding of when it's ok to use copies of the same reference, and when it's not ok is important to writing efficient code.
Related
This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 4 years ago.
when I initialize an array in python like so and then assign an element to a different value and then print out the 2d list
dist = [[0]*3]*3
dist[1][1] = 1
It gives me this
[[0,1,0],[0,1,0],[0,1,0]]
Can someone explain to me why the whole column has changed instead of just the specific element?
Because each of your 3 columns are references to the same object.
If you had created dist like so:
dist = [[0,0,0],[0,0,0],[0,0,0]]
you would see the behavior you expect.
This question already has an answer here:
How to zip lists in a list [duplicate]
(1 answer)
Closed 4 years ago.
I wanted to create a new list with the same positions of each sublist
is it possible to create?
list1 = [[1,2,3],[4,5,6],[a,b,c],[d,e,f]
I wanted to create a new list
new_list = [[1,4,a,d],[2,5,b,e],[3,6,c,f]]
You can use zip():
list(zip(*list1))
This question already has answers here:
Understanding slicing
(38 answers)
Closed 8 years ago.
Suppose I have a list [1,2,3,4,5]. Now I want to pass the elements of this list, starting from 3rd element to a method.
i.e. i want to invoke:
myfunction([3,4,5])
How can I do this in python. tried passing mylist[2], but it doesn't quite work this way it seems.
slicing.
mylist = range(1,6)
>> [1,2,3,4,5]
myfunction(mylist[2:])
>> [3,4,5]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 9 years ago.
I am trying to make a list of lists of about 5000 lists and it keeps messing up.
right now I just do this:
array = [[]]*5000
for line in f2:
a = line.split()
grid = int(a[0])
array[grid].append(a[1])
print Counter(array[0]).most_common(10)
the problem is when I make the counter it does it as if the whole array of lists was actually just one list. Is there something obvious that I am doing wrong?
Thanks
Using [[]]*5000, you are creating 5000 reference to the same list in your outer list. So, if you modify any list, it will modify all of them.
You can get different lists like this:
a = [[] for _ in xrange(5000)]