Understanding `list[x][y]` - python

I was looking at making a maze game in python, and on an online tutorial there was a list that seemed to be indexed twice. I understand that list[x] pulls the x item from that list, but I don't know how double brackets like list[x][y] work.
Does it pull the y of the x of list?
Can someone please tell me what it's called so I can further research it?

Yes, you are correct. Imagine we have a list with sublists in them. For example:
a = [[1,2,3],[4,5,6],[7,8,9]]
If we want to access one of those numbers, let's say 6, and assign it to the variable b, we would select it like this:
b = a[1][2]
The one means the 2nd element in the list a and the two would mean the 3rd element in the list we selected from a.
I hope this solves your problem and happy coding!

Related

Accessing current instance of python for loop

in Python how does one call the current position of a interation in a "for x, y, z in list():"?
I have a list with numerous subsists consisting of three variables, but I don't quite know how to call the current position of the loop - ie. Call the value of X in the current iteration of the loop.
I am really new to coding and python so any help would be appreciated greatly - I read something about enumerate but that just confused me and I didn't know whether it would help or how to reference it or use it
currently my loop looks like:
for step_num, direction, changes in movements:
with movements being a list consisting of multiple sub lists with three variables each (one numeric and two alphanumeric). my goal is to be able to reference the current variable of the current sublist being iterated through - I'd read something about enumerate potentially being able to help with finding the current value of a sub list variable, however I don't know how to use it as such, if indeed it can be used like that, especially since the output is being used in a turtle window to draw different objects.
As it stands I'm not sure how to make it happen, so the functions making drawings don't know how to draw the current value in the loop
you can use enumerate(list) like so:
for index, y in enumerate(list):
print(index) # position of loop
print(y) # item in list
I'm making some assumptions here, but are you looking for something like this?
# iterate through each sublist in `movements`
# and also get its index
for ix, movement in enumerate(movements):
for step_num, direction, changes in movement: # extract the values from each sublist
... # do stuff with the index and corresponding values
P.S.: If you are new to SO, please take sometime to learn how to produce a minimal reproducible example. It will only increase your chances of getting a quick and useful response.

How does the expression r[[2,3],[2,3]] access an array in Python

I am new to Python and I am learning from an online course. In a quiz, I was asked this question:
The choices
I have correctly solved it, but I was looking at the other choices wondering how they would select indices in an array and I have understood all but the 2nd choice:
r[[2,3],[2,3]]
I tried searching for it online but I haven't found an example like it. Is this correct Python syntax? And if so, how would it access an array?
yes , it will return r[2,2] and r[3,3] as answer.
In this syntax, first list will correspond to the row coordinates and the second list will correspond to its respective col coordinates.
For eg:
r = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print(r[[2,3],[2,3]])
output will be
array([11, 16])
The syntax is probably incorrect. The list index must be integer, not tuple

Double variable in print function possible matrix operation

I have a simple list stated as a non-matrix. The print function calls on the variable in two manners than a location. I am unable to find materials explaining why the answer rendered is rendered when ran. I would like a walk through if possible so I can understand.
I've tried to look the solution up though I may be using improper keywords.
lst=[3,1,-2]
print(lst[lst[-1]])
I understand its a list with the variable lst containing 3,1,-2 in the 0,1,2 location left to right or -1,-2,-3 location right to left. What does the lst in brackets do that causes the answer to be 1 instead of -2?
The term lst[-1] returns -2 and now when this passed again to lst, like lst[-2] it returns second last element that is 1, if you want -2 as your answer just do print(lst[-1])

Need help understanding some code (Beginner)

I am trying to learn about while and for loops. This function prints out the highest number in a list. But, I'm not entirely sure how it works. Can anyone break down how it works for me. Maybe step by step and/or with a flowchart. I'm struggling and want to learn.
def highest_number(list_tested):
x=list_tested[0]
for number in list_tested:
if x<number:
x=number
print(x)
highest_number([1,5,3,2,3,4,5,8,5,21,2,8,9,3])
One of the most helpful things for understanding new code is going through it step by step:
PythonTutor has a visualizer: Paste in your code and hit visualize execution.
What this is going form the first to the last number and saying:
Is this new number bigger than the one I have? If so, keep the new number, if not keep the old number.
At the end, x will be the largest number.
See my comments for step by step explanation of each line
def highest_number(list_tested): # function defined to take a list
x=list_tested[0] # x is assigned the value of first element of list
for number in list_tested: # iterate over all the elements of input list
if x<number: # if value in 'x' is smaller than the current number
x=number # then store the value of current element in 'x'
print(x) # after iteration complete, print the value of 'x'
highest_number([1,5,3,2,3,4,5,8,5,21,2,8,9,3]) # just call to the function defined above
So basically, the function finds the largest number in the list by value.
It starts by setting the large number (x) as the first element of list, and then keeps comparing it to other elements of the list, until it finds an element which is greater than the largest number found till now (which is stored in x). So at the end, the largest value is stored in x.
Looks like you are new to the programming world. Maybe you should start with some basic concepts, for/while loops are some among which, that would be helpful for you before jumping into something like this.
Here is one of the explanations you may easily find on the Internet http://www.teamten.com/lawrence/programming/intro/intro8.html

Python inserting lists into a list with given length of the list

My problem is, that need a list with length of 6:
list=[[],[],[],[],[],[]]
Ok, that's not difficult. Next I'm going to insert integers into the list:
list=[[60],[47],[0],[47],[],[]]
Here comes the real problem: How can I now extend the lists and fill them again and so on, so that it looks something like that:
list=[[60,47,13],[47,13,8],[1,3,1],[13,8,5],[],[]]
I can't find a solution, because at the beginning i do not know the length of each list, I know, they are all the same, but I'm not able to say what length exactly they will have at the end, so I'm forced to add an element to each of these lists, but for some reason i can't.
Btw: This is not a homework, it's part of a private project :)
You don't. You use normal list operations to add elements.
L[0].append(47)
Don't use the name list for your variable it conflicts with the built-in function list()
my_list = [[],[],[],[],[],[]]
my_list[0].append(60)
my_list[1].append(47)
my_list[2].append(0)
my_list[3].append(47)
print my_list # prints [[60],[47],[0],[47],[],[]]

Categories