Lists in Python

Python tutorial · PySpark.in

List 

A list in Python is an ordered, mutable collection of items that can store multiple data types, such as integers, strings, or even other lists. Python lists are versatile and are one of the most commonly used data structures.

Example → ["Rohan", 22, 5.5, True]

Syntax

PLAINTEXT
1my_list = [item1, item2, item3]
▶ Output will appear here.

1. Creating a List:

Defining a list using square brackets [] and separating items with commas.

PYTHON
1numbers = [10, 20, 30, 40]
2mixed = ["apple", 10, 2.5, True]
3print(numbers)
4print(mixed)
▶ Output will appear here.

2. Accessing List Elements

You can access elements in a list using indexing. Remember, Python uses 0-based indexing.

PYTHON
1fruits = ["apple", "banana", "mango"]
2print(fruits[0]) # apple
3print(fruits[2]) # mango
▶ Output will appear here.

3. Negative Indexing

You can also access elements from the end of the list using negative indexing. The index -1 refers to the last item, -2 to the second last, and so on.

PYTHON
1fruits = ["apple", "banana", "mango"]
2print(fruits[-1]) # mango
3print(fruits[-2]) # banana
▶ Output will appear here.

4. Slicing a List

You can slice a list to extract a part of it using a range of indices.

PYTHON
1fruits = ["apple", "banana", "mango", "orange"]
2print(fruits[1:3]) # ['banana', 'mango']
3print(fruits[:2]) # ['apple', 'banana']
4print(fruits[2:]) # ['mango', 'orange']
▶ Output will appear here.

5. Modifying a List

Since lists are mutable, you can change the values of existing items.

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
▶ Output will appear here.

6. Adding Items to List

Python provides several methods to add items to a list.

append()->Add an item to end of the list.

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4fruits.append("orange")
▶ Output will appear here.

insert()->Adds an item to at a specific postion.

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4fruits.insert(1, "grapes")
▶ Output will appear here.

extend()->Add multiple items from another list.

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4fruits.extend(["papaya", "berry"])
▶ Output will appear here.

7. Removing Items

Python lists offer several methods to remove items:

remove()->Removes tge first occurrence of a specified value.

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4fruits.remove("apple")
▶ Output will appear here.

pop()-> Removes and returns an item at a specified index (default is the last item)

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4fruits.pop(1)
▶ Output will appear here.

del()-> Deletes an item by index or deletes the entire list.

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4del fruits[0]
▶ Output will appear here.

clear()-> Removes all items from the list, leaving it empty..

PYTHON
1fruits = ["apple", "banana", "mango"]
2fruits[1] = "kiwi"
3print(fruits) # ['apple', 'kiwi', 'mango']
4fruits.clear()
▶ Output will appear here.

8. Looping Through List

Using a loop  for to access each item one by one.

PYTHON
1fruits = ["apple", "banana", "mango"]
2for item in fruits:
3 print(item)
▶ Output will appear here.

9. List Methods

Method

Description

append()

Add item

insert()

Insert at position

extend()

Add list to list

remove()

Remove item

pop()

Remove index

clear()

Empty list

sort()

Sort list

reverse()

Reverse list

index()

Find index

count()

Count item

10. Sorting Lists

You can sort a list either in ascending or descending order.

Sort ascending

PYTHON
1numbers = [5, 2, 8, 1]
2numbers.sort()
3print(numbers) # [1, 2, 5, 8]
▶ Output will appear here.

Sort descending

PYTHON
1numbers = [5, 2, 8, 1]
2numbers.sort(reverse=True)
3print(numbers)
▶ Output will appear here.

11. List Comprehension

List comprehension provides a concise way to create lists. It’s a compact version of for loops.

PYTHON
1squares = [x*x for x in range(1, 6)]
2print(squares) # [1, 4, 9, 16, 25]
▶ Output will appear here.

12. Nested Lists

A short, elegant way to create new lists using a single line of code.

PYTHON
1matrix = [
2 [1, 2, 3],
3 [4, 5, 6]
4]
5print(matrix[1][2]) # 6
▶ Output will appear here.

13. Length of List

Returns the total number of items in the list.

PYTHON
1fruits = ["apple", "banana", "mango"]
2print(len(fruits)) # 3
▶ Output will appear here.

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges