• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / List Manipulation in Python

List Manipulation in Python

Author: PFB Staff Writer
Last Updated: August 27, 2020

Overview

List is one of the simplest and most important data structures in Python.

Lists are enclosed in square brackets [ ] and each item is separated by a comma.

Lists are collections of items where each item in the list has an assigned index value.

A list is mutable, meaning you can change its contents.

Lists are very fexible and have many built-in control functions.

Methods of List objects

Calls to list methods have the list they operate on appear before the method name separated by a dot, e.g. L.reverse()


Creation
L = ['yellow', 'red', 'blue', 'green', 'black']
>>>print L
returns: ['yellow', 'red', 'blue', 'green', 'black']

##########

Accessing / Indexing
L[0]  = returns 'yellow'

##########

Slicing
L[1:4]  = returns ['red', 'blue', 'green']
L[2:] 	= returns ['blue', 'green', 'black']
L[:2] 	= returns ['yellow', 'red']
L[-1]  	= returns 'black'
L[1:-1] = returns ['red', 'blue', 'green']

##########

Length - number of items in list
len(L)  = returns 5

##########

Sorting - sorting the list
sorted(L) = returns ['black', 'blue', 'green', 'red', 'yellow']

##########

Append - append to end of list
L.append("pink")

>>> print L
returns: ['black', 'blue', 'green', 'red', 'yellow', 'pink']

##########

Insert - insert into list
L.insert(0, "white")

>>> print L
returns: ['white', 'black', 'blue', 'green', 'red', 'yellow', 'pink']

##########

Extend - grow list

L.extend(L2)

##########

Remove - remove first item in list with value "white"

L.remove("white")

>>> print L
returns: ['black', 'blue', 'green', 'red', 'yellow', 'pink']

##########

Delete

Remove an item from a list given its index instead of its value
del.L[0]

>>> print L
['blue', 'green', 'red', 'yellow', 'pink']

##########

Pop

Remove last item in the list
L.pop()  = returns 'pink'

# remove indexed value from list
L.pop(1) = returns 'green'  

##########

Reverse - reversing the list
L.reverse()

##########

Count

Search list and return number of instances found
L.count('red')

##########

Keyword "in" - can be used to test if an item is in a list
 
if 'red' in L:
    print "list contains", 'red'

##########

For-in statement - makes it easy to loop over the items in a list
for item in L:
    print item

L = ['red', 'blue', 'green']
for col in L:
    print col

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics, Lists Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us