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 two lists with first half and second half elements of a list



Here, we are going to learn how to create two lists with first half and second half elements of a given list in Python?
Submitted by IncludeHelp, on August 03, 2018

Given a list, and we have to create two lists from first half elements and second half elements of a list in Python.

Example:

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

    Output:
    list1: [10, 20, 30]
    list2: [40, 50, 60]

Logic:

  • First take list (Here, we are taking list with 6 elements).
  • To get the elements from/till specified index, use list[n1:n2] notation.
  • To get first half elements, we are using list[:3], it will return first 3 elements of the list.
  • And, to get second half elements, we are using list[3:], it will return elements after first 3 elements. In this example, we have only 6 elements, so next 3 elements will be returned.
  • Finally, print the lists.

Program:

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

# Create list1 with half elements (first 3 elements)
list1 = list [:3]
# Create list2 with next half elements (next 3 elements)
list2 = list [3:]

# print list (s)
print "list : ",list
print "list1: ",list1
print "list2: ",list2

Output

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

Using list[0:3] and list[3:6] instaed of list[:3] and list[3:]

We can also use list[0:3] instead of list[:3] to get first 3 elements and list[3:6] instead of list[3:] to get next 3 elements after first 3 elements.

Consider the program:

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

# Create list1 with half elements (first 3 elements)
list1 = list [0:3]
# Create list2 with next half elements (next 3 elements)
list2 = list [3:6]

# print list (s)
print "list : ",list
print "list1: ",list1
print "list2: ",list2

Output

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


By considering length of the list

Let suppose list has n elements, then we can use list[0:n/2] and list[n/2:n].

Consider the program:

If there are ODD numbers of elements in the list, program will display message "List has ODD number of elements." And exit.

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

# get the length of the list 
n = len(list)

# condition to check length is EVEN or not
# if lenght is ODD, show message and exit
if( n%2 != 0 ):
    print "List has ODD number of elements."
    exit()
    
# Create list1 with half elements (first 3 elements)
list1 = list [0:n/2]
# Create list2 with next half elements (next 3 elements)
list2 = list [n/2:n]

# print list (s)
print "list : ",list
print "list1: ",list1
print "list2: ",list2

Output

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





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.