List Functions in Python

List Functions in Python

List Functions and Tuple Functions

·

2 min read

Tuple Functions

Index

tuple1 = ("Red", "Blue", "Red", "Green")
print(tuple1.index("Blue"))

Terminal:

1

Count

tuple1 = ("Orange", "Green", "Orange", "Blue")
print(tuple1.count("Orange"))

Terminal:

2

List Functions

Clear

.clear() is used to empty the list

colors = ["Red", "Green", "Blue", "Cyan"]
numbers = [1, 2, 3]

colors.clear()
numbers.clear()

print(colors)
print(numbers)

Terminal:

[]
[]

Count

.count(element) Counts number of element inside list.

colors = ["Red", "Blue", "Green", "Blue"]
numbers = [1, 2, 3, 2, 2]

print(colors.count("Blue"))
print(numbers.count(2))

Terminal:

2
3

Copy

Copies list to another variable

colors = ["Red", "Green", "Blue"]
colors_2 = colors.copy()

print(colors_2)

Terminal:

['Red', 'Green', 'Blue']

Extend

appends elements at the end of the list

colors = ["Red", "Green"]
colors_2 = ["Blue", "Orange"]

colors.extend(colors_2)
print(colors)

Terminal:

['Red', 'Green', 'Blue', 'Orange']

Append

Appends an elememnt at the end of the list

colors = ["Red", "Green", "Blue", "Cyan"]
numbers = [1, 2, 3]

colors.append("Yellow")
numbers.append(10)

print(colors)
print(numbers)

Terminal:

['Red', 'Green', 'Blue', 'Cyan', 'Yellow']
[1, 2, 3, 10]

Insert

Adds an element at specified Index

colors = ["Red", "Green", "Blue", "Cyan"]
numbers = [1, 2, 3]

colors.insert(2, "Yellow")
numbers.insert(0, 10)

print(colors)
print(numbers)

Terminal:

['Red', 'Green', 'Yellow', 'Blue', 'Cyan']
[10, 1, 2, 3]

Pop

.pop() removes last element in the list.

colors = ["Red", "Green", "Blue", "Cyan"]
numbers = [1, 2, 3]

colors.pop()
numbers.pop()

print(colors)
print(numbers)

Terminal:

['Red', 'Green', 'Blue']
[1, 2]

Remove

Removes an element from the list

colors = ["Red", "Green", "Blue"]
colors.remove("Red")

print(colors)

Terminal:

['Green', 'Blue']

Reverse

to Reverse List

colors = ["Red", "Green", "Blue"]
numbers = [4, 10, 20, 15, 22]

colors.reverse()
numbers.reverse()

print(colors)
print(numbers)

Terminal:

['Blue', 'Green', 'Red']
[22, 15, 20, 10, 4]

Sort

Sorts List from A to Z or Negative to Positive numbers

colors = ["Red", "Green", "Blue"]
numbers = [-19, 28, -18, -37, 9, 17, 29]

colors.sort()
numbers.sort()

print(colors)
print(numbers)

Terminal:

['Blue', 'Green', 'Red']
[-37, -19, -18, 9, 17, 28, 29]

good time to you!