Skip to content

What is list in Python | Basics

  • by

Python List is the built-in data type used to store multiple items in a single variable. Python has 3 more data types Tuple, Set, and Dictionary to store multiple value data for different purposes.

You can easily create a list by using square brackets, where the elements are placed inside square brackets ([]), separated by commas.

mylist = ["A", "B", "C"]

Example code list in Python

Simple example code. An important point about List is List elements are ordered, changeable, and allow duplicate values.

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




What is list in Python

List items can be of any data type:

list1 = ["A", "B", "C"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False]

Lists are mutable, which means you can modify them by adding, removing, or changing elements. You can access individual elements in a list by using their index. The indexing starts from 0, so the first element has an index of 0, the second element has an index of 1, and so on.

my_list = [1, 2, 3, 4, 5]
print(my_list[0])  # Output: 1
print(my_list[2])  # Output: 3

Lists also support negative indexing, where -1 refers to the last element, -2 refers to the second-to-last element, and so on. Here’s an example:

my_list = [1, 2, 3, 4, 5]
print(my_list[-1])  # Output: 5
print(my_list[-3])  # Output: 3

You can perform various operations on lists, such as appending elements, removing elements, slicing, sorting, and more. Python provides a rich set of built-in methods and functions to work with lists. Here are a few examples:

my_list = [1, 2, 3, 4, 5]

# Append an element to the end of the list

# Remove an element from the list

# Get a sublist using slicing
sub_list = my_list[1:4]

# Sort the list in ascending order

# Find the length of the list
length = len(my_list)

print(my_list)     # Output: [1, 2, 4, 5, 6]
print(sub_list)    # Output: [2, 4, 5]
print(length)      # Output: 5

Lists are versatile and can be used to store and manipulate collections of data efficiently in Python.

Do comment if you have any doubts or suggestions on this Python basics topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading