Data Science Day 8 Lists
Python Lists
Data Science Day 8 Lists
Python Lists
Photo by Peaky Frames on Unsplash
A list is a basic, built-in data structure in Python that is used to store an ordered collection of items.
Lists are similar to arrays (dynamic arrays that allow us to store items of different data types) in other programming languages.
Lists are very flexible and have a number of important features:
Ordered: A list’s items stay in the order they were inserted. This indicates that each element’s position is maintained.
Mutable: Once a list has been created, it can be altered by adding, removing, or changing items. Heterogeneous: Items of various data types, such as integers, strings, floats, booleans, or even other lists, can be stored in the same list.
Indexed: Zero-based indexing is used to access the elements in a list; the first element is at index 0, the second is at index 1, and so forth.
Creating a List: To create a list, enclose a series of items separated by commas in square brackets.
# An empty list
empty_list = []
# A list of integers
numbers = [1, 2, 3, 4, 5]
# A list of strings
fruits = ["apple", "banana", "cherry"]
# A list with mixed data types
mixed_list = ["hello", 123, True, 3.14]
# A nested list (a list containing another list)
nested_list = [1, [2, 3], 4]
Common List Operations and Methods:
Python has a large number of built-in list manipulation methods.
Elements accessed: my_list[index]
Using my_list[start:end] to slice
Adding components using extend(), insert(), and append()
Elements can be removed using remove(), pop(), and clear().
Sorting using sorted() and sort()
locating elements using count() and index()
Len(my_list) is the length.
Example of List Operations:
#Initial list
my_list = [10, 20, 30, 40, 50]
# Access an element
print(my_list[0]) # Output: 10
# Add an element
my_list.append(60)
print(my_list) # Output: [10, 20, 30, 40, 50, 60]
# Remove an element
my_list.remove(30)
print(my_list) # Output: [10, 20, 40, 50, 60]
# Sort the list
my_list.sort(reverse=True)
print(my_list) # Output: [60, 50, 40, 20, 10]
# a list containing strings, numbers and another list
student = [‘Jack’, 32, ‘Computer Science’, [2, 4]]
print(student)
Add Elements to a List From Other Iterables
The list extend() method all the items of the specified iterable, such as list, tuple, dictionary or string , to the end of a list. For example,
numbers = [1, 3, 5,7]
print(‘Numbers:’, numbers)
even_numbers = [2, 4, 6,8,10]
print(‘Even numbers:’, numbers)
# adding elements of one list to another
numbers.extend(even_numbers)
print(‘Updated Numbers:’, numbers)
Output:
Numbers: [1, 3, 5, 7]
Even numbers: [1, 3, 5, 7]
Updated Numbers: [1, 3, 5, 7, 2, 4, 6, 8, 10]
Change List Items
We can change the items of a list by assigning new values using the = operator.
For example,
colors = [‘Yellow’,’Red’, ‘Black’, ‘Green’]
print(‘Original List:’, colors)
# change the first item to ‘Brown’
colors[0] = ‘Brown’
# change the third item to ‘Violet’
colors[2] = ‘Violet’
print(‘Updated List:’, colors)
Output
Original List: [‘Yellow’, ‘Red’, ‘Black’, ‘Green’]
Updated List: [‘Yellow’, ‘Red’, ‘Violet’, ‘Green’]
Python List Methods
Python has many useful methods that make it really easy to work with lists.
Methods
Description
append()
Adds an item to the end of the list
extend()
Adds items of lists and other iterables to the end of the list
insert()
Inserts an item at the specified index
remove()
Removes the specified value from the list
pop()
Returns and removes item present at the given index
clear()
Removes all items from the list
index()
Returns the index of the first matched item
count()
Returns the count of the specified item in the list
sort()
Sorts the list in ascending/descending order
reverse()
Reverses the item of the list
copy()
Returns the shallow copy of the list
메타데이터
- post_id
- 32ca2eb4b730
- slug
- data-science-day-8-lists-32ca2eb4b730
- url
- https://medium.com/@tharunbs352/data-science-day-8-lists-32ca2eb4b730
- canonical_url
- https://medium.com/@tharunbs352/data-science-day-8-lists-32ca2eb4b730
- author_url
- https://medium.com/@tharunbs352
- status
- ok
- fetched_at
- 2026-06-24 04:09:36