That creates a list of one element with just the node object. Presumably results is expected to be a list.
Hello,
Please review the code in bold.
My question is in both examples,
Example 1.
python_topics = ["variables", "control flow", "loops", "modules", "classes"]
length = len(python_topics)
index = 0
while index < length:
print("I am learning about " + python_topics[index])
index += 1
Example 2.
favorite_fruit = "blueberry"
last_char = favorite_fruit**[len(favorite_fruit) - 1]**
Output: Y
My question is, their a way to describe what the square brackets are doing? Would you say that the square brackets are used to access or transfer the value of the variable? Hopefully this question makes sense. Thank you very much.
Tuple declaration using square brackets
python - What do the square brackets around parameters mean in the official documentation? - Stack Overflow
Meaning of square brackets
double square brackets side by side in python - Stack Overflow
Videos
If you have a list
l = ["foo", "bar", "buz"]
Then l[0] is "foo", l[1] is "bar", l[2] is buz.
Similarly you could have a list in it instead of strings.
l = [ [1,2,3], "bar", "buz"]
Now l[0] is [1,2,3].
What if you want to access the second item in that list of numbers? You could say:
l[0][1]
l[0] first gets you the list, then [1] picks out the second number in it. That's why you have "square bracket next to square bracket".
Square brackets are used to define lists, but also to get things from lists.
When you have a list of lists and want something from an inner list, you need to get that inner list (using brackets) and then get the desired thing inside (using brackets again).
lol = [[1, 2, 3], [4, 5, 6]]
lol[1]
# [4, 5, 6]
lol[1][0]
# 4
Variable points to a nested list, where length of outer as well as inner list is 1.
In [9]: lst = [[1]]
In [10]: len(lst)
Out[10]: 1
In [11]: len(lst[0])
Out[11]: 1
Appending list to an empty list,
In [12]: lst = []
In [13]: lst.append([1])
In [14]: lst
Out[14]: [[1]]
It depends on what data type some_var is holding. If some_var is a primitive data type, say integer, then lst is a list of list of an integer. But if some_var is a list itself, then lst is a list of list of list