Yandex

Python Lists



In real life, we often need to keep track of multiple items together. For example, a list of groceries, names of your friends, or favorite movies.

In Python, we use something called a list to store many items in one place.

What is a List?

A list is a collection of items. These items are stored in a specific order, and you can add, change, or remove items whenever you want.

Think of it like a shopping list you can edit!

How to Create a List

You can create a list by putting items inside square brackets [ ], separated by commas.

fruits = ["apple", "banana", "cherry"]

What Does This Mean?

  • fruits is the name of the list.
  • It contains 3 items: "apple", "banana", and "cherry".
  • The items are stored in the order you write them.

Where Are Lists Used?

Lists are used everywhere when you need to store multiple values together. Some examples:

  • Shopping lists
  • Student names in a class
  • Scores in a game
  • Tasks in a to-do app

Nature of Lists

  • Lists can hold different types of items (strings, numbers, even other lists!).
  • Lists are ordered. This means the position of each item is important.
  • Lists can be changed (we can add, remove, or update items).

Basic List Operations (CRUD)

Create

Creating a new list:

colors = ["red", "green", "blue"]

Read (Access Items)

We can read items by their position (called an index). Remember, counting starts from 0!

print(colors[0])
red

Here, colors[0] gives us the first item, which is "red".

Update (Change Items)

We can change an item in the list:

colors[1] = "yellow"
print(colors)
['red', 'yellow', 'blue']

We changed the second item ("green") to "yellow".

Delete (Remove Items)

We can remove items from the list using del:

del colors[2]
print(colors)
['red', 'yellow']

We deleted the third item ("blue"). Now only two colors are left.

More Useful List Tricks

Adding Items

Use append() to add a new item at the end:

colors.append("purple")
print(colors)
['red', 'yellow', 'purple']

Checking the Length of a List

Use len() to find how many items are in the list:

print(len(colors))
3

Summary

  • A list holds multiple items in one place.
  • Items are ordered and can be changed.
  • You can create, read, update, and delete items easily.

Now that you know lists, you're ready to start managing collections of data in your Python programs!



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M