Retrieving list elements in Python [duplicate] - python

This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
I'm a beginner attempting to learn Python. I am familiarising myself with the list data type; I've defined the following list:
>>> numbers = [1, 2, 3, 4]
Typing:
>>> numbers[0]
1
>>> numbers[1]
2
>>> numbers[2]
3
>>> numbers[3]
4
Given this, why do I get the following when I attempt to retrieve the following list elements:
>>> numbers[0:3]
[1, 2, 3]
Why isn't the list element '4' included in the response from the interpreter?
Thank you for your help.

Slice notation does not include the last element (similar to the range() function in that respect). If you want to include the last element, simply omit an index. Also, the default start is the beginning, so you don't need 0 there either:
>>> numbers[:]
[1, 2, 3, 4]
Note that this is a (shallow) copy of numbers. If you save a reference to it, you can mutate it without affecting the original numbers.

That's how slicing works in Python. To quote a tutorial:
Note how the start is always included, and the end always excluded.
This makes sure that s[:i] + s[i:] is always equal to s.
The example uses a string, but slicing works the same way with lists.

numbers[0:3] list from 0 up to 3 but 3 is excluded (like range(0,3))

Related

Python for loop weird bug [duplicate]

This question already has answers here:
Why can I use a list index as an indexing variable in a for loop? [duplicate]
(6 answers)
Are for-loop name list expressions legal?
(2 answers)
Closed last year.
So in a quiz, I have given the question what the execution of this block of code gives (The one under).
a = [0, 1, 2, 3]
for a[0] in a:
print(a[0])
I had selected error but to my surprise, it actually works and is able to print all the elements inside the list. But how?
firstly the element getting variable (usually i) is shadowing the actual variable on top of that we are getting the first element inside a number so how is it working.
a[0] is type of a so it can be used to iterate over the array but as in look you are assigning value in a[0] it's value will change to last element of the array.
a = [0,1,2,3]
for a[0] in a:
print(a[0]
Will result in:
0
1
2
3
But now printing a will give you modified array:
print(a)
[3, 1, 2, 3]

Why, when slicing the list, the number of the items in a list is always more than the ending index? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
Why for example when slicing a list with square brackets in this way
spam = [1, 2, 3]
When we print(spam[0:2]) the 3 is not typed while the index of that is 2?
This style of indexing is just very beautiful and comfortable. There're lots of cases where this can be useful. The easiest one is when you have to do something on two halves of an array:
spam = [1, 2, 3, 4, 5]
center_index = len(spam) / 2
func(spam[center_index:])
func(spam[:center_index])
In this example, you don't have to think about +1/-1
Because Python slicing is inclusive # start and exclusive # the end

Why doesn`t list[:][0] get me the first row of the list? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
For the following:
list=[[2, 3, 5], [7, 8, 9]]
Why does [list[0][0], list[1][0]] represent the first row ([2, 7]), but the command list[:][0] or list[0:2][0] returns the first column ([2, 3, 5])?
The way I see it list[:][0] should get all the possible values for the first parameter (that is 0 and 1) and 0 for the second, meaning it would return the first row. Instead what it does is return the first column and I can't understand why.
In python, the [a:b:c] syntax creates a new list. That is,
list = [1,2,3]
print(list[:])
is going to print a list, not a value.
Therefore, when you say list[:][0] you are making a copy of the original list (list[:]) and then accessing item 0 within it.
Of course you know, item 0 of the original list (list[0]) is another list.
I think you want:
[sl[0] for sl in list]
Elaboration:
This is called a "comprehension." It is a compact special syntax for generating lists, dicts, and tuples by processing or filtering other iterables. Basically {..}, [..], and (..) with an expression inside involving a for and optionally an if. Naturally, you can have multiples (like [x for x in y for y in z]) of the for, and multiples of the if.
In your case, it's pretty obvious you want a list. So []. You want to make the list by taking the first item from each sublist. So [sl[0] for sl in list].
Here's a more-detailed article: http://carlgroner.me/Python/2011/11/09/An-Introduction-to-List-Comprehensions-in-Python.html

python slicing strings vs lists: inclusive/noninclusive slice index [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
Suppose I was slicing a list and a string:
num_list = [1, 2, 3, 4]
print(num_list[2:]) # => [3, 4]
text_string = 'This is a test string'
print(text_string[5:] # => 'is a text string'
So, when I slice the list, the first slice index is inclusive, but when I slice the string, the slice index is noninclusive. What is it like this? Why does string slicing not follow the same ruleset that list slicing appears too?
My understanding of list slicing is that the left slice index is always inclusive, whilst the right slice index is only inclusive when it is left blank.
They both behave exactly the same.
For some reason you expect the list's indexes to start from 0 and
the string's indexes to start from 1.
The fact is that they both start at 0.
As #Mark suggested in the comments, strings and lists indexing from Python's documentation.
But it is the same!
Insert indices before each element:
[0=1,1=2,2=3,3=4][2:] means: from index 2 until end
[0=T,1=h,2=i,3=s,4= ,5=i,6=s,...][5:] means: from index 5( in this case from the letter 'i') until the end.

slice operator understanding [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
good primer for python slice notation
I am a little confused as to what the slice operator does in python. Can anyone explain to me how it works?
The slice operator is a way to get items from lists, as well as change them. See http://docs.python.org/tutorial/introduction.html#lists.
You can use it to get parts of lists, skipping items, reversing lists, and so on:
>>> a = [1,2,3,4]
>>> a[0:2] # take items 0-2, upper bound noninclusive
[1, 2]
>>> a[0:-1] #take all but the last
[1, 2, 3]
>>> a[1:4]
[2, 3, 4]
>>> a[::-1] # reverse the list
[4, 3, 2, 1]
>>> a[::2] # skip 2
[1, 3]
The first index is where to start, the (optional) second one is where to end, and the (optional) third one is the step.
And yes, this question is a duplicate of Explain Python's slice notation.

Categories

Resources