1 minute read

CRUD of List of Python

Add

  • append
  • Insert

Delete

  • del (with index number)
  • delete (with value)
  • pop (with index number and want to use the value
food = ["pasta", "pizza", "bibimbap", "Pazun"]
print(food)



# Add on the list

# append
new_food = "fired rice"
food.append(new_food)
print(food)

# insert
food.insert(1,"smoke meat")
print(food)



# Delete element

# pop removes the item at a specific index and returns it.
name = ["harry", "Potter", "emma", "marry"]
print(name.pop()) # marry
print(f"I just removed the name : {name.pop(2)}") # emma
print(name) # ['harry', 'Potter']

# del removes the item at a specific index:
name = ["harry", "Potter", "emma", "marry"]
del name[0]
print(name) # ['Potter', 'emma', 'marry']

Sort

  • sort
  • sorted
  • reverse
  • len
# sort : change original data
food = ["pasta", "pizza", "bibimbap", "Xianmi"]
food.sort()
print(food)

# sorted : maintain original order of a list
food = ["pasta", "pizza", "bibimbap", "Xianmi"]
print(sorted(food))

# reverse
food = ["pasta", "pizza", "bibimbap", "Xianmi"]
print(food)
food.reverse()
print(food) # ['Xianmi', 'bibimbap', 'pizza', 'pasta']

# len
print(len(food)) #4

Copy list

names = ["tom", "paul", "mike", "jay", "johny", "lyn"]
new_names = names[:]
print(names)

You might think new_names = names is also working
However, if you add new element to new_names
original names would be changed as well

Categories:

Updated: