What does python do to find -2 items in the list? does iterate through the list?
a = [1, 2, 3, 4, 5]
print(a[-2])
I don't understand how it works.
Python programming language supports negative indexing of arrays, something which is not available in arrays in most other programming languages. This means that the index value of -1 gives the last element, and -2 gives the second last element of an array. The negative indexing starts from where the array ends.
(-) negative index iterates from last element to first element.
a = [1, 2, 3, 4, 5]
if you do a[-1] it returns last element (5).
if you do a[-2] it returns second last element(4) of the list and so on.
Related
In python, I learned that it is natural to start an index set from 0. I.e.) if there is an array, the first element is not the 1st but the 0th element.
My question is what about when you count the number of things.
For example, if there is an array, {3, -1, 2, 5, -7}, what is the number of the elements of the array? Is it 5 elements or 4 elements?
Is the number of the elements 0 or 1, when there is {3}, an array with a single element?
I'm not a computer engineer so have less knowledge about programming.
if there is an array, {3, -1, 2, 5, -7}, what is the number of the
elements of the array? Is it 5 elements or 4 elements? Is the number
of the elements 0 or 1, when there is {3}, an array with a single
element?
The list
a = []
is an empty list and has 0 elements.
The list
a = [1]
is a nonempty list and has 1 element. The first and only element can be accessed via a[0].
The list
a = [1, 2, 3, 4, 5]
is a nonempty list and has 5 elements. The first element is accessed via a[0], the second by a[1], the third by a[2], ..., the fifth by a[4].
In python, I learned that it is natural to start an index set from 0.
I.e.) if there is an array, the first element is not the 1st but the
0th element.
The first element is the first element. In computer science (not just in Python), we often use zero-based indexing whereby the initial/first element of a sequence is assigned the index 0, the second element is assigned the index 1, and so on.
In general, with zero-based indexing, given some nonempty sequence, the
nth element is assigned the index n - 1
When we want to access the third element of a, we know that the third element is assigned the index 2 and so we write a[2] to access it.
When you have an array like this
>>> array = [ 3, -1, 2, 5, -7]
Now I print the number of elements of the array with the help of the len function
>>> print(len(array))
5
the result is 5, since array programming always starts at index 0. At first it can seem confusing.
[ 3, -1, 2, 5, -7] -> [0,1,2,3,4]
Av = [32,5,3,1,3,4,5]
A = Av[0:int(len(Av)/2)]
B = Av[int(len(Av)/2):-1]
print(A,B)
When I run this block of code, I get
[32, 5, 3] [1, 3, 4]
The last value of Av is 5. But it is not showing up on the list B..
It's because you have sliced the list till -1 which means the last element of the array and is excluded from the sliced array.
To get an array sliced till the end leave the end part of the slice code empty. Like this -
B = Av[int(len(Av)/2):]
In python when you use index slicing Av[a:b] you get the elements from position a (included) to position b (excluded). Because -1 refers to the last position, if you do Av[a:-1], the last element won't be included.
If you want to include the last element, just omit the final index -1, that is use Av[a:]. Your code should be like this:
Av = [32,5,3,1,3,4,5]
A = Av[0:int(len(Av)/2)]
B = Av[int(len(Av)/2):]
print(A,B)
Take a look at https://stackoverflow.com/a/509295/7558835 answer. It explains very clearly how does index slicing work.
When you use the slice [x:-1], it doesn't include the value at index -1 (last position), because it is non-inclusive.
Instead, use [x:], which will give a slice that includes x and all values to its right:
B = Av[int(len(Av)/2):]
Output:
>>>B
>>>[1, 3, 4, 5]
Let's say I've got a nested list like this:
mylist = [[],[],[]]
and I want to insert elements at the end of the second nested list:
mylist[1].insert(-1, 1)
mylist[1].insert(-1, 2)
The output i expected was:
[[], [1, 2], []]
but instead I got:
[[], [2, 1], []]
Can somebody explain this to me? I thought the index -1 always pointed to the last position of a list.
according to documentation (https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), the first argument of the insert method is the index of the element before which to insert...and -1 designates the last element of a list: so by calling insert(-1,...) the element you insert will always become the next to last element of your list.
this is easy to veryfy. if you insert yet another element
mylist[1].insert(-1, 3)
you will notice the resulting list becomes
[[], [2, 3, 1], []]
so should probably use append instead. or calculate the index dynamically like
mylist[1].insert(len(mylist[1]), 3)
If a list has for example 3 elements, they are numbered from 0 to 2 (i.e. 0, 1, 2).
If you want use negative indices, they are numbered from -3 to -1 (i.e. -3, -2, -1).
Now, if you want insert a new element at position -1, it is the same, as inserting at position 2, i.e. the inserted element will become element[2].
But element[2] will be then an element of the 4-element list, so its current position in the negative notation is not -1 but -2:
element[-4], element[-3], element[-2], element[-1]
From this page,
list.insert(index, element) means you insert element at index index. .insert(-1, value) means inserting value at the last element of the list (index=len(lst)-1). So
mylist[1].insert(1, 1)
mylist[1].insert(1, 2)
should solve the problem.
Another approach is to use append as the other one said.
mylist[1].append(1)
mylist[1].append(2)
I'm slicing lists in python and can't explain some results.
Both of the following seem natural to me:
>>>[0,1,2,3,4,5][1:4:1]
[1, 2, 3]
>>>[0,1,2,3,4,5]
[::-1] == [5,4,3,2,1,0]
However,
>>>[0,1,2,3,4,5][1:4:-1]
[]
thought I expected it to be [3,2,1]. Why does it produce [ ]? Why does it not reverse the list? What happens first inside python, the step or the slicing?
I also found that
>>>[0,1,2,3,4,5][-3:-6:-1]
[3,2,1]
The third number in the slice is the step count. So, in [0,1,2,3,4,5][1:4:-1], the slicing starts at 1 and goes DOWN by 1 until it is less than 4, which is immediately is. Try doing this:
>>> [0,1,2,3,4,5][4:1:-1]
[4, 3, 2]
If you are slicing this then slicing will look like this [start:end:step]. For this one:
>>> [0,1,2,3,4,5][1:4:1]
[1, 2, 3]
It is staring from index 1 to index 3 because it exlcudes the index 4 with 1 step at a time.
You are getting an empty list in the second one because you are stepping -1 from 1st index. So this will be the solution.
>>> [0,1,2,3,4,5][4:1:-1]
[4, 3, 2]
This works because you are taking an index from 4 to one with -1 step forward.
Very basic question here (I've just started with Python).
I have a list object. It contains five numbers [3,6,2,3,1]
I want find the sum of the first, third and fourth numbers in the list.
What is the syntax?
You can for instance sum elements #1, #3, and #4 with the flexible expression
sum(my_list[i] for i in (0, 2, 3))
The index of the first element is 0 [not 1], etc., i.e. my_list[0] is the first element (with value 3, in the original question), etc.
The items of a list are numbered like this:
a = [3, 6, 2, 3, 1]
^ ^ ^ ^ ^
index 0 1 2 3 4
To access the item with the index i, use a[i]. From here, you should be able to figure out how to sum the desired items.
Just write the index in brackets. Note that the index starts with zero:
lst[0] + lst[2] + lst[3]
In some cases, you can use the sum function and select a slice of the list. For example, to get the sum of the first, third, and fifth element, use:
sum(lst[::2])
You can access an element of a Python list by index by appending [list_index] to the list object (replace list_index with the index you want). For example:
my_list_object = [3,6,2,3,1]
my_sum = my_list_object[0]+my_list_object[2]+my_list_object[3]