• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » What is a Python List explain with Example
Next →
← Prev

What is a Python List explain with Example

By Dinesh Thakur

A Python list is a sequence of values. It is the most basic mutable type data structure responsible for an ordered set of values and a versatile collection of data objects. An index identifies each item or sometimes in a list.

The list is the most frequently used datatype in Python. Lists are dynamic because they can contain as many variables and handle various data types at a time.

Lists in Python are similar to arrays in Java, which are ordered sets of characters, with the difference that the elements of a list can have any type. Lists and strings and other things that behave like ordered sets are called sequences. Python offers a series of built-in functions and integrated to perform different types of operations methods.

We’ll be covering the following topics in this tutorial:

  • How to Create Lists in Python
  • Accessing items in a python list
  • Deleting or Removing Elements from the python List
  • Members list in python
  • Python List Methods

How to Create Lists in Python

In Python programming, to create a list, place any number of items (elements) inside a square bracket [and], separated by commas, similar to function arguments and they may be of the same type or different type. Following is an example of a List in Python. For example:

# empty list
    MyList = []
# list of integers
    MyList = [100, 101, 102, 103]
# list of string
    MyList = [ 'India', 'Canada', 'UK']
# list with mixed datatypes
   MyList = [100, "India", 103]
# nested list
    MyList = ["UK", [100, 200, 300]]

So that you may see a list termed as ‘myList’ was made with a few elements.

Lists in python are not any indexed. This implies you can access individual elements in a list as you’d do within an array. Here’s an example:

Whenever you Make a List in Python, you will need to make a difference between:

Lists that include strings, in which each item within the list will set within quotations:

ListName = [‘Item1′,’Item2′,’Item3’,].

Lists that contain numeric values, where each item will not place within quotes:

ListName = [Item1, Item2, Item3,….].

Lists that will have a combination of strings and numeric values.

Accessing items in a python list

There are lots of ways to access the elements of a list.

Index Operator []

The syntax for access single element of a list is the same as you would do in an array. The expression inside the square brackets [] specifies the index. List indexes are zero-based; that is, they range from 0 to len(MyList)-1. The index value can be used as a variable, so it’s called an indexed variable. For example

MyList1 = [100, 101, 102, 103]
MyList2 = ['India', 'Canada', 'UK']
print (MyList1[0])
# Any integer expression can be used as an index
print MyList2[1:3]
After execution above code, it produces the following result.
100
['Canada', 'UK']

Negative indexing

Python allows list items can be accessed with positive or negative index. The index value -1 refers to the last element of the index, or -2 can be second previous element and so on.

MyList = ['India', 'Canada', 'UK']
MyList [-1]
'UK'
 MyList [-3]
'India'
Here is an example of the python list that cannot be accessed beyond the upper boundaries.
MyList[5]
File "<stdin>", line 1, in <module>
IndexError: list index out of range
As seen above, when we are tried to access MyList index 5, an IndexError was thrown.

List Slicing [start : end]

We can access a list range by using the slicing operator (:).

MyList = ['e','c','o','m','p','u','t','e','r']
MyList [2:5]  
['o', 'm', 'p']
MyList [:-5]  
['e', 'c', 'o', 'm']
 MyList [5:]    
['u', 't', 'e', 'r']
MyList [:]    
['e', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r']

Deleting or Removing Elements from the python List

Python provides the remove() or del() method through which we can delete one or more elements from a list. We use the ‘del’ statement when you want to remove items by index, or a range of indices.

MyList = ['US', 2015, 'India' , 2011];
print MyList
# delete one item
   del MyList[2];
# delete multiplt items
   del MyList[1:3]  
# delete entire list
    del MyList       
print "After Deleting the List : "
print MyList

We can use remove() method deleting first matching element in the list.
MyList = [‘US’, 2015, ‘India’ , 2011];
MyList.remove(‘India’)
print MyList
[‘US’, 2015 , 2011];

Members list in python

Logical operator IN that tests whether an element is a member of a sequence and it also works with lists and other sequences:

>>> Fruit = [‘Apple’, 'Gava', 'Orange', 'Banana']
>>> ’Apple’ in Fruit
True
>>> ’Grapes’ in Fruit
False

Since “Apple” is a member of the Fruit list in the operator returns true. Once Grape is not on the list, in return false.

We can also use in combination with not ping to test whether an element is not a member of a list:

‘‘Grapes‘‘ not in Fruit
True

Python List Methods

The list class methods for manipulating a list are tabulated below.

       Python List Methods

You’ll also like:

  1. List & Explain Various Components of an SRS
  2. What is Python? | Introduction to Python Programming
  3. Python Features | Main Features of Python Programming Language
  4. Python Operators and Operands – Types of Operators in Python
  5. Fibonacci Series in Python
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.