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.

Top comments (0)