Sitemap

Learn to Code in Python-Lists

3 min readMar 3, 2024

--

What is a Python List ??

A Python list is a collection of data objects that maintains the order of its elements and allows for modifications. Unlike arrays, which are restricted to holding elements of a single type, lists in Python can accommodate a variety of object types, enabling a mixture of data within the same list.

Press enter or click to view image in full size

As shown in the example, a list in Python can hold different kinds of things, like numbers, words, or even other lists. This makes lists in Python really useful and flexible for programming.

List Creation

Definition: List creation refers to the process of initializing or defining a list in Python, which involves enclosing elements within square brackets [ ].

Example:

# Creating a list of numbers 
numbers = [1, 2, 3, 4, 5]
# Creating a list of strings
fruits = ['apple', 'banana', 'orange']
# Creating a list of mixed data types
mixed_list = [1, 'hello', True, 3.14]

Accessing an Element:

Definition: Accessing an element in a list involves retrieving a specific item from the list using its index position.

Example:


# Creating a list of strings
fruits = ['apple', 'banana', 'orange']
# Accessing the first element of the list
first_element = fruits[0] print(first_element)
# Output: 'apple'
# Accessing the last element of the list
last_element = fruits[-1] print(last_element)
# Output: 'orange

Adding and Removing Elements:

Definition: Adding elements involves inserting new items into a list, while removing elements entails deleting existing items from a list.

Example:

fruits = ['apple', 'banana', 'orange']
# Adding an element to the end of the list
fruits.append('grape')
print(fruits)
# Output: ['apple', 'banana', 'orange', 'grape']
# Removing an element by value
fruits.remove('banana')
print(fruits)
# Output: ['apple', 'orange', 'grape']

Looping in Lists:

Definition: Looping through lists involves iterating over each element in the list to perform a specific action.

Example:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# Output:apple
# banana
# cherry

List Comprehension:

Get Tanvi Jain’s stories in your inbox

Join Medium for free to get updates from this writer.

Definition: List comprehension provides a concise way to create lists based on existing lists or iterables.

Example:

# Using list comprehension to create a list of squares 
squares = [x**2 for x in range(1, 6)] print(squares)
# Output: [1, 4, 9, 16, 25]

Sort Lists:

Definition: Sorting lists involves arranging the elements of a list in ascending or descending order.

Example:

numbers=[2,1,4,5,3]
# Sorting a list of numbers in ascending order
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4, 5]

Copy Lists:

Definition: Copying lists involves creating a new list that contains the same elements as the original list.

Example:

# Copying a list using the copy() method 
numbers_copy = numbers.copy()
print(numbers_copy)
# Output: [1, 2, 3, 4, 5]

Join Lists:

Definition: Joining lists involves combining the elements of multiple lists into a single list.

Example:

# Joining two lists using the extend() method 
more_fruits = ['pineapple', 'mango']
fruits.extend(more_fruits)
print(fruits)
# Output: ['apple', 'orange', 'grape', 'pineapple', 'mango']

List Methods:

Definition: List methods are built-in functions in Python that can be applied to lists to perform various operations.

Press enter or click to view image in full size

I hope you all get basic idea about lists in python.

Thank you for reading !! Keep Learning !! Keep Growing!!

--

--

Tanvi Jain
Tanvi Jain

Written by Tanvi Jain

Software Developer | MSCS Grad Student @SBU | Ex-Developer at TCS Digital | Ex-Intern at TCSion