This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Closed 7 years ago.
I was wondering how to reverse a list of lists in python. For example,
Original: list = [[1,2,3],[4,5,6],[7,8,9]]
Output: new_list = [[7,8,9],[4,5,6],[1,2,3]]
Right now, I am trying this:
new_list = reversed(list)
However, this doesn't seem to work.
In [24]: L = [[1,2,3],[4,5,6],[7,8,9]]
In [25]: L[::-1]
Out[25]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
Related
This question already has answers here:
Convert tuple to list and back
(11 answers)
Closed 2 years ago.
a = [1, 2, 3]
b = [4, 5, 6]
res = next(zip(a, b))
print(res)
Output:
(1,4)
how to get every pair in aa a separate list?
Expected Output:
[1,4]
[2,5]
[3,6]
you can zip the items in a list and unpack them in a list comp.
new_list = [[x,y] for x,y in zip(a,b)]
print(new_list)
[[1, 4], [2, 5], [3, 6]]
the issue with your solution is your first turning your lists into an iterator (correct) but you are only calling the first value with next
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 2 years ago.
I tried the following code:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in arr:
if x > 3:
arr.remove(x)
print(arr)
It gives the following output:
[1, 2, 3, 5, 7, 9]
While instead i thought it would remove from the array every element bigger than 3. Why is that? And how can i make it remove every element bigger than a certain amount?
Modifying a list that you're in the process of iterating over will frequently yield unexpected results. A better option is to create a new list:
arr = [x for x in arr if x <= 3]
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
Is there an easy way to convert a 1D list into a 2D list with a given row length?
Suppose I have a list like this:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
I want to convert the above list into a 3 x 3 table like below:
myList = [[1,2,3],[4,5,6],[7,8,9]]
I know I can accomplish this by creating a new 2D list called myList2 and inserting the element into MyList2 using 2 nested for loops. Like MyList2[x][y] = myList[i] i++
I think there should be a better way to do it (without using external libraries or modules)
Thanks!
Using list comprehension with slice:
>>> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> n = 3
>>> [myList[i:i+n] for i in range(0, len(myList), n)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Modifying list while iterating [duplicate]
(7 answers)
Closed 8 years ago.
I'm trying to modify a loop function that contains over a hundred lines of code.
If I have a loop in python that goes through a list, why does a for item in list: loop only finish half way through? Shouldn't it keep looping until all items are popped?
For example if I have this list:
l1 = [1,2,3,4,5,6]
for item in l1:
l1.pop(0)
print l1
No matter how many items the list contains, it will only finish when 50% of the list has been popped:
OUTPUT FOR CODE ABOVE:
[2, 3, 4, 5, 6]
[3, 4, 5, 6]
[4, 5, 6]
Process finished with exit code 0
Can I still pop each item in the list without changing the for item in l1: loop?
You could create a copy of the list and pop from the original one:
In [1]: l1 = [1,2,3,4,5,6]
In [2]: for item in l1[:]:
...: l1.pop(0)
...: print l1
...:
[2, 3, 4, 5, 6]
[3, 4, 5, 6]
[4, 5, 6]
[5, 6]
[6]
[]
However, this is certainly not elegant.
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
Suppose I have a list of lists, like [[1, 2], [3, 4], [5, 6], [7, 8]]. What is the most elegant way in python to get [1, 2, 3, 4, 5, 6, 7, 8]?
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
Or:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
res=[]
for item in mylistOfList:
res+=item