Fundamentals 8 min read

Master Python Lists: From Basics to Advanced Operations

This tutorial walks you through Python list syntax, common operations such as adding, modifying, searching, deleting, sorting, and nesting, complete with code examples and output screenshots, helping beginners and intermediate programmers deepen their understanding of list handling.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python Lists: From Basics to Advanced Operations

Hello everyone, I'm Cai, and today I'll share knowledge about Python lists

1. List Syntax

Example: namesList = ['xiaoWang','xiaoZhg','xiaa'] Unlike C arrays, Python lists can hold elements of different types.

testList = [1, 'a']

2. List Operations (Add, Delete, Modify, Search)

1) Adding elements

Use append() to add an element.

# Define list A with three elements
A = ['rr', 'rag', 'rte']
print("-----Before adding, list A-----")
for tempName in A:
    print(tempName)
temp = input('Please enter a student name to add:')
A.append(temp)
print("-----After adding, list A-----")
for tempName in A:
    print(tempName)

Result:

2) Modifying elements

Use index to specify which element to change.

# Define list A
A = ['rr', 'rag', 'rte']
print("-----Before modification, list A-----")
for tempName in A:
    print(tempName)
A[1] = 'Lu'
print("-----After modification, list A-----")
for tempName in A:
    print(tempName)

Result:

3) Searching elements

Common methods: in, not in, index, count. in: returns True if element exists. not in: returns True if element does not exist.

# List to search
A = ['rr', 'rag', 'rte']
findName = input('Enter the name to search:')
if findName in A:
    print('Found the content in the list')
else:
    print('Not found')

Result (found):

Result (not found):

4) Deleting elements

del

removes by index.

Name = ['加勒比海盗','骇客帝国','第一滴血','霍比特人','速度与激情']
print('------Before deletion------')
for tempName in Name:
    print(tempName)
del Name[2]
print('------After deletion------')
for tempName in Name:
    print(tempName)

Result:

pop

removes the last element.

Subject = ['数学','语文','英语','地理','历史']
print('------Before deletion------')
for tempSubject in Subject:
    print(tempSubject)
del Subject[2]  # delete second element
print('------After deletion------')
for tempSubject in Subject:
    print(tempSubject)

Result:

remove

deletes by value.

Subject = ['数学','语文','英语','地理','历史']
print('------Before deletion------')
for tempSubject in Subject:
    print(tempSubject)
Subject.remove('英语')
print('------After deletion------')
for tempSubject in Subject:
    print(tempSubject)

Result:

5) Sorting

sort()

orders the list (default ascending). Use reverse=True for descending. reverse() reverses the list.

a = [1,4,2,3]
print(a)
a.reverse()
print(a)  # reversed
a.sort()
print(a)  # sorted ascending
a.sort(reverse=True)
print(a)  # sorted descending

Result:

3. Nested Lists

1) List nesting

Lists can contain other lists.

Letter = [['A','B'], ['C','D','E'], ['F','R']]

2) List of dictionaries

Example: roster.

pep1 = {'name':'蔡同学','school':'北京大学'}
pep2 = {'name':'陈作同','school':'中山大学'}
pep_list = [pep1, pep2]
for pepo in pep_list:
    print(pepo)

Result:

3) Dictionary with list values

Example: book tags.

book = {'title':'现代艺术150年','tags':['数学','历史学']}
for tags in book['tags']:
    print(tags)

Result:

4. Summary

This article explains Python list fundamentals, common operations, and typical issues with solutions, helping readers better understand list usage.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

OperationsData StructuresTutorialListcoding
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.