Working with Multiple Data - Lists
In this article, the theme is “multiple data.”
You’ll learn how to manage many pieces of data together at once.
Lists
By using lists, you can manage multiple values together.
A list is written by placing values separated by commas between [ and ].
member = ["Maruko", "Tama", "Maruo"]
print(member) # Check all data
# ['Maruko', 'Tama', 'Maruo']
To access a single value in a list, use its index.
The first element is [0], the second is [1], the third is [2], and so on.
member = ["Maruko", "Tama", "Maruo"]
print(member[0]) # Maruko
print(member[1]) # Tama
print(member[2]) # Maruo
Lists are mutable, which means you can change their values at any time.
member = ["Maruko", "Tama", "Maruo"]
member[1] = "Hamaji"
member[2] = "Butaro"
print(member)
# ['Maruko', 'Hamaji', 'Butaro']
Common List Methods
Lists provide many useful methods for adding, removing, and manipulating elements.
Here are some of the most commonly used ones.
append (Add an element)
Use append() to add an element to the end of a list.
member = ["Maruko", "Tama", "Maruo"]
member.append("Hanawa")
print(member)
# ['Maruko', 'Tama', 'Maruo', 'Hanawa']
insert (Insert an element)
Use insert() to add an element at a specific position.
member = ["Maruko", "Tama", "Maruo", "Hanawa"]
member.insert(3, "Migiwa")
print(member)
# ['Maruko', 'Tama', 'Maruo', 'Migiwa', 'Hanawa']
extend (Add all elements from another iterable)
Use extend() to add all elements from another list (or iterable).
member = ["Maruko", "Tama", "Maruo"]
others = ["Nagasawa", "Fujii", "Yamane"]
member.extend(others)
print(member)
# ['Maruko', 'Tama', 'Maruo', 'Nagasawa', 'Fujii', 'Yamane']
remove (Remove an element)
Use remove() to delete a specific value from a list.
member = ["Maruko", "Tama", "Maruo"]
member.remove("Tama")
print(member)
# ['Maruko', 'Maruo']
pop (Remove and return an element)
pop() removes and returns an element at the specified index.
If no index is given, it removes and returns the last element.
member = ["Maruko", "Tama", "Maruo"]
print(member.pop(1)) # Tama
print(member.pop()) # Maruo (last element)
print(member)
# ['Maruko']
clear (Remove all elements)
Use clear() to remove all elements from a list.
member = ["Maruko", "Tama", "Maruo"]
member.clear()
print(member)
# []
index (Get the first index of a value)
index() returns the index of the first occurrence of a value.
member = ["Maruko", "Noguchi", "Tama", "Noguchi", "Maruo", "Noguchi"]
print(member.index("Noguchi"))
# 1
count (Count occurrences of a value)
count() returns how many times a value appears in a list.
member = ["Maruko", "Noguchi", "Tama", "Noguchi", "Maruo", "Noguchi"]
print(member.count("Noguchi"))
# 3
sort (Sort elements)
sort() arranges elements in ascending (dictionary) order.
member = ["Maruko", "Tama", "Maruo"]
member.sort()
print(member)
# ['Tama', 'Maruko', 'Maruo']
reverse (Reverse the order)
reverse() simply reverses the order of the list.
member = ["Maruko", "Tama", "Maruo"]
member.reverse()
print(member)
# ['Maruo', 'Tama', 'Maruko']
Slicing
By using slicing, you can extract part of a list (or tuple).
Use : to specify the start and end positions.
The slice includes elements from the start index up to (but not including) the end index.
list[start:end] # From start to one before end
Here are some examples:
member = ["Maruko", "Tama", "Maruo", "Ohno", "Sugiyama"]
print(member[1:3]) # ['Tama', 'Maruo']
print(member[3:]) # ['Ohno', 'Sugiyama']
print(member[:2]) # ['Maruko', 'Tama']
print(member[-2:]) # ['Ohno', 'Sugiyama']
What’s Next?
Thank you for reading!
In the next article, we’ll continue with “Working with Multiple Data (Part 2).”
Stay tuned! 🚀
Top comments (0)