[ Web Proxy ]
URL:
Viewing: https://programminginpython.com/python-lists-add-append-modify-remove-slice/ [Back]  [Original]

Python Lists Add, Append, Modify, Remove, Slice - Programming In Python
Skip to content
Home Python Lists Add, Append, Modify, Remove, Slice

Python Lists Add, Append, Modify, Remove, Slice

Python Lists  Add, Append, Modify, Remove, Slice [Python Lists Add, Append, Modify, Remove, Slice] Python Lists Add, Append, Modify, Remove, Slice

Hello folks, welcome back to programminginpython.com. In this post, we will learn about Lists in Python. Here we perform the basic operations on a list like adding items to a list, appending an item at the end of the list, modifying the value of the item in the list, removing an item from the list, removing all the items from the list, and some slicing operations. So basically all the Python List operations are performed.

Master the basics of data analysis in Python. Expand your skillset by learning scientific computing with numpy.

Take the course on Introduction to Python on DataCamp here https://bit.ly/datacamp-intro-to-python

Program on Github

You can watch the video on YouTube here.

Python Lists- Code Visualization

Task :

To perform add, append, modify, remove, and slice operations on a list.

Approach :

  • Define a list lst = [] with some sample items in it.
  • Find the length of the list using len() function.
  • Append an item to the list using append() function.
  • Change the value of an item in a list by specifying an index and its value lst[n] = 'some value'
  • Perform slice operation, slice operation syntax is lst[begin:end]
  • Leaving the beginone empty lst[:m] gives the list from 0 to m.
  • Leaving the endone empty lst[n:] gives the list from n to end.
  • Giving both begin and end lst[n:m] gives the listfrom n to m.
  • An advanced slice operation with 3 options lst[begin:end:step]
  • Leaving both begin and end empty and giving a step of -1, lst[::-1] it reverses the whole list.
  • To remove an element using slice lst[:n] = [] removes all elements from 0 to n, similarly lst[m:] removes all elements from m to end and lst[m:n] = [] removes all elements from m to n.
  • The whole list can be removed by lst[:] = []

Program on GitHub

Ad:
Learn Python Programming Masterclass Enroll Now.
Udemy

Program :

lst = ['abc', 'def', 'ghi', 'jklm', 'nopqr', 'st', 'uv', 'wxyz', '23', 's98', '123', '87']

# prints the length of the list
print('Length of the list is : ', len(lst))

# appends an item to the list
lst.append('sdd')
print('\nThe list appended a new element "sdd" at the end \n', lst)

# changes the value of an item in a list
lst[0] = 'aaa'
print('\nThe value of the first element in the list is changed to "aaa" \n', lst)

# Slicing
# shows only items starting from 0 upto 3
print('\nList showing only items starting from 0 upto 3\n', lst[:3])

# shows only items starting from 4 to the end
print('\nList showing only items starting from 4 to the end\n', lst[4:])

# shows only items starting from 2 upto 6
print('\nList showing only items starting from 2 upto 6\n', lst[2:6])

# reverse all items in the list
print('\nList items reversed \n', lst[::-1])

# removing items from list
lst[:1] = []
print('\nFirst element is removed from the list \n', lst)

# removing whole list
lst[:] = []
print('\nComplete list removed \n', lst)

Output :

Python Lists  Add, Append, Modify, Remove, Slice [Python Lists Add, Append, Modify, Remove, Slice]Python Lists Add, Append, Modify, Remove, Slice

That’s it for the post, we have now successfully performed all the Python List operations.

Program on GitHub

Ad:
Learn Python Programming Masterclass Enroll Now.
Udemy

Online Python Compiler

Tagged: list add list append list items list modify list operations lists slicing

Post navigation

Leave a Reply Cancel reply

author:
email:
url:
comment:
ak_hp_textarea:

Programming In Python 2026. Powered By BlazeThemes.

Web Proxy Viewer  |  New URL  |  Original Page