Create & Access Lists0%

Create & Access Lists

Beginner8 min readUpdated: 2026-09-03
Study Materials

Create & Access Lists

Access elements in a list using index brackets or slices.


Key Concepts & Detailed Explanation

undefined


Code Examples & Output

Python
courses = ["Python", "Web Dev", "Data Science", "Cyber Security", "AI"]
 
# Indexing
print("First course:", courses[0]) # Python
print("Last course:", courses[-1]) # AI
 
# Slicing [start:stop]
print("Top 2:", courses[:2]) # ['Python', 'Web Dev']
print("Middle:", courses[1:4]) # ['Web Dev', 'Data Science', 'Cyber Security']
 
# Check membership
if "Python" in courses:
print("Python is offered at MSK Institute!")

Expected Output:

Output
First course: Python
Last course: AI
Top 2: ['Python', 'Web Dev']
Middle: ['Web Dev', 'Data Science', 'Cyber Security']
Python is offered at MSK Institute!

Best Practices & Common Pitfalls

Accessing an index beyond len(list) - 1 raises an IndexError. Always check length or use slices (slices never raise IndexError).


Practice Quiz

1. What does list[-2] access?

  • A) The second element from the start
  • B) The second element from the end
  • C) An error
  • D) The second slice
Answer: B Explanation: Negative indices count backwards from the end.

2. What happens when you slice beyond list bounds (e.g. [1, 2][0:100])?

  • A) IndexError
  • B) Returns available items without error
  • C) None
  • D) Crashes Python
Answer: B Explanation: Slicing gracefully clamps to available bounds without throwing IndexError.

Practice Challenge

From a list of 7 numbers, extract the first 3, the last 3, and every alternate number using slices.

Next Lesson

Add & Remove Items

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
List IntroductionAdd & Remove Items

Practice Quiz

Test your understanding of this lesson with 1 questions. Each question has one correct answer.

PrevNext