Automating the creation of a list of lists - Python - python

I have to create a list of lists that is summarized below:
list_of_lists = [[0,1],[0,2],[0,3]....[0,N]]
Basically just need the first element of each sub-list to be 0, and the second element to be 1 more than the prior value of the second element.
The value for N is about 2000, so obviously I do not want to type out the whole thing. Is there a simple way to automate with Python?
Thank You

You can use simple list comprehension with range:
>>> N = 5
>>> [[0, i] for i in range(1, N + 1)]
[[0, 1], [0, 2], [0, 3], [0, 4], [0, 5]]

Related

Slice Operators | Catching the 2nd element in every sub list | Python [duplicate]

This question already has answers here:
Extract first item of each sublist
(8 answers)
Closed 1 year ago.
I have a list of lists, each sub-list contain exactly 2 floats:
my_list = [[0, 1], [0, 1], [0, 1]]
Is there a one liner slice operation; so as to not have a for loop?
Desired Output:
> [[1], [1], [1]]
Then, I would like to merge these sub-lists as elements into one list. Outputting, as dtype list:
> [1, 1, 1]
Failed attempt:
d_[:][1]
> [[0, 1]]
You can use list comprehension
ans = [x[1] for x in my_list]
Full answer:
my_list = [[0, 1], [0, 1], [0, 1]]
ans = [x[1] for x in my_list]
print(ans)
>>> [1, 1, 1]

How to iterate over a list that has duplicate values?

This is probably a very basic question but I dont know what I have to search for to find the answer for it:
I have this code:
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
list.append(list[0])
for i in list:
i.append(0)
print(list)
This List will later be used as coordinates for a curve. I need to duplicate the first coordinate at the end to get a closed curve.
If I then want to add a third value to each coordinate in the list the first and last item in list will be iterated over twice:
[[0, 1, 0, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0, 0]]
I am guessing they have the same memory address and thereby the append-function is applied to the same object at this address once for the first index and once for the last.
What is this phenomenon called ? what is the easiest way to get the list like this:
[[0, 1, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0]]
Thank you for your help
You can do a list comprehension:
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
list.append(list[0])
list = [x + [0] for x in list]
print(list)
# [[0, 1, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0]]
EDIT: The trick here is, using x + [0] within the list comprehension. This way new lists are created, thus you do not append 0 to the same list twice (Hattip to #dx_over_dt)
The problem you have with your approach is, that the first and last element of your list refers to the very same object. You can see this, when you print i and list for every iteration:
for i in list:
i.append(0)
print(i)
print(list)
So for the first and last i in your loop, you will append a 0 to the very same list.
You could stick to your approach appending a copy of the first element:
list.append(list[0].copy())
The simplest answer is to add the 0's before appending the closing point.
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
for i in list:
i.append(0)
list.append(list[0])
print(list)
It's the tiniest bit more efficient than a list comprehension because it's not making copies of the elements.

Removing a specific list from an array

I have this list
list = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 1], [2, 2], [2, 0]]
I want to take 2 integers
row = 2 and column = 1
Combine them
thing = (str(row) + str(", ") + str(column))
then I want to remove the list
[2, 1]
from the array. How would I do this?
EDIT: The language is Python
First of all, don't name your list list. It will overwrite the builtin function list() and potentially mess with your code later.
Secondly, finding and removing elements in a list is done like
data.remove(value)
or in your case
data.remove([2, 1])
Specifically, where you are looking for an entry [row, column], you would do
data.remove([row, column])
where row and column are your two variables.
It may be a bit confusing to name them row and column, though. because your data could be interpreted as a matrix/2D array, where "row" and "column" have a different meaning.

Optimal method to find the max of sublist items within list

I have a multidimensional list in the format:
list = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]
How do I obtain the maximum value of the third value of all the sublists. In pseudo code:
max(list[0][2], list[1][2], list[2][2])
I know this can be done via iterating over the list and extracting the third value into a new list, then simply performing max(list), but I'm wondering if this can be done using a lambda or list comprehension?
Just use max with a generator expression:
>>> lst = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]
>>> max(l[2] for l in lst)
3
Also, don't name your variables list, you are shadowing the type.
Use zip function to get the list of columns then use a simple indexing in order to get the expected column:
>>> lst = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]
>>>
>>> max(zip(*lst)[-1]) # in python 3.x max(list(zip(*lst))[-1])
3
One another alternative and more pythonic approach is passing a key function to max to get the max item based on the key function. In this case you can use itemgetter(-1) in order to get the max item based on intended index then since the max() function returns the whole item from your list (sub-list) you can get the expected item by indexing:
>>> from operator import itemgetter
>>> max(lst, key=itemgetter(-1))[-1]
3
Or more functional:
>>> key_func = itemgetter(-1)
>>> key_func(max(lst, key=key_func))
3
applying max on the list will return the maximum list, which isn't what you want. You could indeed use a list comprehension to extract the third element, and then apply max on that comprehension:
>>> lst = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]
>>> max([x[2] for x in lst])
3

change list elements [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python List Index
result=[range(3)]*2
for i in range(len(result)):
result[i][2]=4*i
print result
I would expected [[0, 1, 0], [0, 1, 4]]
Why do I get [[0, 1, 4], [0, 1, 4]]
Thank you!
When you do [range(3)] * 2, it makes a list with two references to the same list inside, so modifying result[0] and result[1] each modify both.
Use [range(3) for i in range(2)] to make a list with two different results of range(3) in it.
List "result" is: [[0, 1, 2], [0, 1, 2]]
Your iteration is: "for i in range(len(result))"
len(result) is: 2
range(2) is: [0,1]
meaning:
first time:
result[0][2]=4*0
second time:
result[1][2]=4*1
which gives you the result [[0, 1, 4], [0, 1, 4]]
This is what is doing step by step.
If you add a "break" to the iteration you see the result is [[0, 1, 0], [0, 1, 0]]
The "result" list works by reference. When it is called, it is pointing to the same object.

Categories