Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python programs

Python | Create a list from the specified start to end index of another list



Here, we are going to learn how to create a list from the specified start to end index of another (given) list in Python.
Submitted by IncludeHelp, on August 03, 2018

Given a list, start and end index, we have to create a list from specified index of the list in Python.

Example 1:

    Input:
    list : [10, 20, 30, 40, 50, 60]
    start = 1
    end = 4

    Logic to create list with start and end indexes:
    List1 = list[start: end+1]

    Output:
    list1: [20, 30, 40, 50]

Example 2:

    Input:
    list : [10, 20, 30, 40, 50, 60]
    start = 1
    end   = 6

    Logic to create list with start and end indexes:
    list1 = list[start: end+1]

    Output:
    Invalid end index

Logic:

  • Take a list, start and end indexes of the list.
  • Check the bounds of start and end index, if start index is less than 0, print the message and quit the program, and if end index is greater than the length-1, print the message and quit the program.
  • To create a list from another list with given start and end indexes, use list[n1:n2] notation, in the program, the indexes are start and end. Thus, the statement to create list is list1 = list[start: end+1].
  • Finally, print the lists.

Program:

# define list 
list = [10, 20, 30, 40, 50, 60]

start = 1
end = 4

if ( start < 0):
    print "Invalid start index"
    quit()

if( end > len(list)-1):
    print "Invalid end index"
    quit ()

# create another list
list1 = list[start:end+1]

# printth lists 
print "list : ", list
print "list1: ", list1

Output

    list :  [10, 20, 30, 40, 50, 60]
    list1:  [20, 30, 40, 50]

Test with the invalid index

Size of the list is 6, and indexes are from 0 to 5, in this example the end index is invalid (which is 6), thus program will print "Invalid end index" and quit.

Note: Program may give correct output if end index is greater than the length-1 of the list. But, to execute program without any problem, we should validate the start and end index.

# define list 
list = [10, 20, 30, 40, 50, 60]

start = 1
end = 6

if ( start < 0):
    print "Invalid start index"
    quit()

if( end > len(list)-1):
    print "Invalid end index"
    quit ()

# create another list
list1 = list[start:end+1]

# printth lists 
print "list : ", list
print "list1: ", list1

Output

    Invalid end index





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.