FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
2.3k views
in Python Programming by Goeduhub's Expert (9.3k points)
List slicing (selecting parts of lists)
by (100 points)
Got to know a new logic for shifting a list.

4 Answers

2 like 0 dislike
by Goeduhub's Expert (9.3k points)
edited by
 
Best answer

List slicing (selecting parts of lists)- 

Syntax- String_name[Start:Stop:Step]

This means that slice operator(:) will do slicing, which start from indexs "Start" will go up to "Stop"  in "Step" of steps.

Default Value of Start index is first element of list, Stop index is last element of list and Step is 1. If any one of three value is not given then it will consider default value of that index.

Section 2: Selecting a sublist from a list

lst = ['a', 'b', 'c', 'd', 'e']   

Here index of list named lst  is like 0,1,2,3,4 and list also have negative index like last element starts from -1 , then second last -2 and so on.  

lst[2:4]   # this will print elements at index 2, 3 . Here start index is 2 that is inclusive and last index is 4 that is exclusive and step by default 1.

# Output: ['c', 'd']

lst[2:]

# Output: ['c', 'd', 'e']

lst[:4]

# Output: ['a', 'b', 'c', 'd']

Section 1: Using the third "step" argument

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

lst[::2]

# Output: ['a', 'c', 'e', 'g']

lst[::3]

# Output: ['a', 'd', 'g']

Section 3: Reversing a list with slicing

a = [1, 2, 3, 4, 5]

# steps through the list backwards (step=-1)

b = a[::-1]

# built-in list method to reverse 'a'

a.reverse()

if a = b:

print(True)

print(b)

# Output:

# True

# [5, 4, 3, 2, 1]

Section 4: Shifting a list using slicing

def shift_list(array, s):

"""Shifts the elements of a list to the left or right.

Args:

array - the list to shift

s - the amount to shift the list ('+': right-shift, '-': left-shift)

Returns:

shifted_array - the shifted list"""

# calculate actual shift amount (e.g., 11 --> 1 if length of the array is 5)

s %= len(array)

# reverse the shift direction to be more intuitive

s *= -1

# shift array with list slicing

shifted_array = array[s:] + array[:s]

return shifted_array

my_array = [1, 2, 3, 4, 5]

# negative numbers

shift_list(my_array, -7)

>>> [3, 4, 5, 1, 2]

# no shift on numbers equal to the size of the array

shift_list(my_array, 5)

>>> [1, 2, 3, 4, 5]

# works on positive numbers

shift_list(my_array, 3)

>>> [3, 4, 5, 1, 2]

Accessing values in nested list-

Starting with a three-dimensional list:

alist = [[[1,2],[3,4]], [[5,6,7],[8,9,10], [12, 13, 14]]]

 Accessing items in the list:

print(alist[0][0][1])

#2

#Accesses second element in the first list in the first list

print(alist[1][1][2])

#10

#Accesses the third element in the second list in the second list

Performing support operations:

alist[0][0].append(11)

print(alist[0][0][2])

#11

#Appends 11 to the end of the first list in the first list


Iteration in List

The for loop is used to iterate over the list elements: 

l1=[1,2,3,4]

for i in l1: 

 print(i) 

Output 1 2 3 4 

Here, i will iterate over the elements of the List and contains each element in each iteration.

Python Tutorial 

Machine Learning Tutorial 

AI Tutorial

Free Online Tutorials

by (454 points)
Please explain shifting. I am unable to understand the what is happening.
by (103 points)
today i learn list slicing in the class
by Goeduhub's Expert (2.2k points)
Section 4: Shifting a list using slicing- basically we are shifting list elements to the left or right.
here again i am taking example-
I have defined a function-
def shift(a,s):
  s=s%len(a)
  s=s*-1
  shifted=a[s:]+a[:s]
  return shifted
Note- In above function : a is list which we want to shift and  s the amount to shift the list ('+': right-shift, '-': left-shift)
Now I am calling this shift() function:-
a=[1,2,3,4,5]
sl(a,2)  # output  [3, 4, 5, 1, 2] we are shifting by 2 right shift
sl(a,-2)  # output [4, 5, 1, 2, 3] we are shifting by 2 left shift
by (202 points)
i have doubt in following



alist[0][0].append(11)

print(alist[0][0][2])

#11

#Appends 11 to the end of the first list in the first list
by (102 points)
Topic list slicing
Date 23/04/20
Learned about slicing list shift aaray  but i have on  doubt in shifting list using slicing can you explain me
by (422 points)
List slicing is  same as compare to string slicing.
by (294 points)
I have learnt about list slicing it is similar to string slicing but lists are mutable and also learned how to access it from list
by (294 points)
I have learnt about list slicing and also learned how to access it from list
by (135 points)
NameError:name 'alist' is not define
by Goeduhub's Expert (2.2k points)
@Nehasaran  You have not declared alist
Need to write
alist = [[[1,2],[3,4]], [[5,6,7],[8,9,10], [12, 13, 14]]]
by Goeduhub's Expert (2.2k points)
@shagun
Doubt in following--
alist[0][0].append(11)

print(alist[0][0][2])

#11

#Appends 11 to the end of the first list in the first list
-----------------------
In this our alist is
alist = [[[1,2],[3,4]], [[5,6,7],[8,9,10], [12, 13, 14]]]
when you print
print(alist[0][0]) then output would be [1,2]
thats why when you append something like this
alist[0][0].append(11)
print(alist[0][0])  #[1,2,11]
print(alist)
[[[1, 2, 11], [3, 4]], [[5, 6, 7], [8, 9, 10], [12, 13, 14]]]
output would be
by (100 points)
In this article, i learned about slicing on python lists. How we can extract only useful elements from a vast data using slicing and using nested list.
0 like 0 dislike
by (391 points)
Date: 22/04/2020

Topic: List Slicing

Today i have learned to access morethan one element. We can use slicing which has start:stop OR start:stop:step. We can easily solve the query related to loop. We can also get reverse elements or can shift it.
0 like 0 dislike
by (182 points)

Module:-list slicing       Date:-24/04/2020

In this module I have learnt about list slicing it is similar to string slicing but lists are mutable that means we can update and delete list elements and also i have learnt how to skip elements from list ,i mean from reversing list.I have learnt also how to access element from nested list and how to shift a list left to right or right to left.

Thank you !

0 like 0 dislike
by (232 points)

                                                                                                              Topic: List Slicing

Today i have learned how to access more than one element in python. We can use slicing which has start:stop OR start:stop:step. We can easily solve the query related to loop. We can also get reverse elements or can shift it.

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 412 views
0 like 0 dislike
1 answer 339 views
1 like 0 dislike
6 answers 4.1k views
asked Oct 30, 2019 in Python Programming by Goeduhub Goeduhub's Expert (9.3k points)

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...