Master Python Lists: Essential Operations, Indexing, and Manipulation
This tutorial explains Python's list data structure, covering creation, indexing, length retrieval, element addition, insertion, removal with pop, and replacement, illustrated with clear code snippets and screenshots for easy learning.
Python List Usage
1. List
Python's built‑in list is an ordered collection that can be modified by adding or removing elements.
For example, a list of classmates' names can be represented as:
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates)The variable classmates is a list.
len() Function
1. Get the number of elements in a list
classmates = ['Michael', 'Bob', 'Tracy']
print(len(classmates))Indexes start at 0. Access elements with classmates[0], classmates[1], etc. Accessing an out‑of‑range index raises IndexError. The last element is at len(classmates) - 1 or can be accessed with -1.
print(classmates[-1])Similarly, -2 and -3 retrieve the second‑last and third‑last elements; -4 would be out of range.
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[-1])
print(classmates[-2])
print(classmates[-3])2. Append elements to the end of a list
classmates = ['Michael', 'Bob', 'Tracy']
classmates.append('Adam')
print(classmates)You can also insert an element at a specific position, e.g., index 1:
classmates.insert(1, 'Jack')
print(classmates)pop() Function
1. Remove the last element
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates.pop())
print(classmates)2. Remove an element at a specific index with pop(i)
classmates.pop(1)
print(classmates)3. Replace an element by assigning to its index
classmates = ['Michael', 'Bob', 'Tracy']
classmates[1] = 'Sarah'
print(classmates)Lists can contain elements of different types, for example: L = ['Apple', 123, True] Lists can also contain other lists:
s = ['python', 'java', ['asp', 'php'], 'scheme']
print(len(s))The variable s has four elements; s[2] is itself a list. To access 'php' you can use p[1] after extracting the sublist or s[2][1]. Thus s behaves like a two‑dimensional array.
An empty list has length 0:
L = []
len(L)2. Summary
This article introduces Python's list data structure, covering creation, indexing, length retrieval, appending, inserting, popping, and element replacement, with code examples and screenshots to aid understanding.
Using Python makes these concepts easy to grasp for learners.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
