Big Data Lecture # 5
Big Data Lecture # 5
duplicate members.
duplicate members.
duplicate members.
List
he list data type is a built-in data structure used to store collections of items. Lists are
ordered, mutable (modifiable), and can contain elements of different data types, including
integers, floats, strings, and even other lists. Lists are defined by enclosing the elements
within square brackets [ ] and separating them with commas.
Ordered: Lists maintain the order of elements as they are inserted. You can access elements
by their index, and they will be retrieved in the same order.
Mutable: Lists can be modified after they are created. You can change, add, or remove
elements from a list.
Heterogeneous: Lists can contain elements of different data types. For example, you can
have a list with integers, strings, and even nested lists.
Dynamic: Lists can grow or shrink in size dynamically as elements are added or removed.
Iterable: You can iterate over the elements of a list using loops or other iterable methods.
Declaring the List
thislist = ["apple", "banana", "cherry"]
print(thislist)
Access Items
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Negative Indexing
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range of Indexes
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Change Item Value
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Append Items
thislist = ["apple", "banana", "cherry"]
Remove Specified Item
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Sort List Alphanumerically
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)