Movatterモバイル変換


[0]ホーム

URL:


Skip to content
June 4, 2025
Latest
Home »Python Lists – Add, Append, Modify, Remove, Slice
Python Lists – Add, Append, Modify, Remove, Slice
Python Lists – Add, Append, Modify, Remove, SlicePython Lists – Add, Append, Modify, Remove, Slice

Hello folks, welcome back toprogramminginpython.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 onDataCamp herehttps://bit.ly/datacamp-intro-to-python

Program on Github

You can watch the video on YouTubehere.

Python Lists- Code Visualization

Task :

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

Approach :

  • Define a listlst = [] with some sample items in it.
  • Find the length of the list usinglen() function.
  • Append an item to the list usingappend() function.
  • Change the value of an item in a list by specifying an index and its valuelst[n] = 'some value'
  • Perform slice operation, slice operation syntax islst[begin:end]
  • Leaving the begin one emptylst[:m] gives the list from 0 to m.
  • Leaving the end one emptylst[n:] gives the list from n to end.
  • Giving both begin and endlst[n:m] gives the list from n to m.
  • An advanced slice operation with 3 optionslst[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 slicelst[:n] = []  removes all elements from 0 to n, similarlylst[m:] removes all elements from m to end andlst[m:n] = [] removes all elements from m to n.
  • The whole list can be removed bylst[:] = []

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 listprint('Length of the list is : ', len(lst))# appends an item to the listlst.append('sdd')print('\nThe list appended a new element "sdd" at the end \n', lst)# changes the value of an item in a listlst[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 3print('\nList showing only items starting from 0 upto 3\n', lst[:3])# shows only items starting from 4 to the endprint('\nList showing only items starting from 4 to the end\n', lst[4:])# shows only items starting from 2 upto 6print('\nList showing only items starting from 2 upto 6\n', lst[2:6])# reverse all items in the listprint('\nList items reversed \n', lst[::-1])# removing items from listlst[:1] = []print('\nFirst element is removed from the list \n', lst)# removing whole listlst[:] = []print('\nComplete list removed \n', lst)

Output :

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

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Special Offer

Subscribe to our newsletter!

Great Deals

Ads

Categories


[8]ページ先頭

©2009-2025 Movatter.jp