DEV Community

Cover image for Python Lists in 5 minutes
Suresh Kumar
Suresh Kumar

Posted on

Python Lists in 5 minutes

In Python, a list is an ordered collection of items, which can hold a variety of data types including primitive types like integers, floats, and strings.

Lists are mutable, meaning you can modify their contents after creation.

Creating a List with Primitive Types

emptylist = [] #this is empty list

my_list = [1, 3.14, "hello", 42]

Looping Through a Python List

You can use a loop (like for) to traverse and print each element of the list.
for item in my_list:
print(item)

Basic Operations on Python Lists

Accessing Python List elements:

print(my_list[0]) # Access the first item

Modifying an element to Python List:

my_list[1] = 2.71 # Change second element

Adding elements to Python List:

my_list.append(100) # Add 100 to the end

Removing elements from Python List:

my_list.remove("hello") # Remove the string "hello"

Python List length:

print(len(my_list)) # Get the number of elements

Conclusion
Python lists are versatile and used for storing and manipulating primitive types. Whether accessing, modifying, or adding/removing elements, Python makes list operations simple and efficient.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay