DEV Community

MI Shajib
MI Shajib

Posted on

1 1

Python List

# Author - MI SHAJIB

# Python List / or any other language called by array

bazar = ["alu", "potol", "morich"]

print(bazar)

# List item update / replace

print(bazar[0])

bazar[0] = "fish"

print(bazar)
# we can change any item using negative index example

print(bazar[-1])

bazar[-1] = "cucumber"

print(bazar)

# bazar[3] = "cream"  # It will give list assignment error (list assignment index out of range) That's why we use append() method of python

# print(bazar)


# Add / Append new item with append() method. append() method add item end of the list
bazar.append("cream")
print(bazar)

# If we want to add new item using index number then we can use insert() method
bazar.insert(0, "oil")
print(bazar)

# we can also add two list
bazar2 = ['mango', "salt", "spray"]

print(bazar2)

final_bazar_list = bazar2 + bazar

print(final_bazar_list)

print('\n \n \n \n')


# Remove list item using del keyword
del final_bazar_list[2]  # Here we want to delete 2 index item / spray
print(final_bazar_list)  # Successfully delete the given item

print('\n \n \n')

# Remove list item using remove() method
final_bazar_list.remove('potol')  # Here we want to remove potol
print(final_bazar_list)  # Successfully removed item

print('\n \n \n')


# If we want to remove end item of the list then we can use pop() method. Also we can store removed item by using this pop() method
# Here we can remove last item and store the removed item into variable
removed_item = final_bazar_list.pop()
print(final_bazar_list)
print(removed_item)

print('\n \n \n')

# We can also define empty list
empty_list = []
print(empty_list)

print('\n \n \n')


# we can also convert string into list
string = "shajib, zihan, akter"
string_list = string.split(',')
print(string_list)
print(type(string_list))

# we can also split string by space
string2 = "polash, fahad, akter"
string_list2 = string2.split(' ')
print(string_list2)
print(type(string_list2))

print('\n \n \n')

# We can find length of list by len() method
print(final_bazar_list)
print(len(final_bazar_list))
print('\n \n \n')


# We can append multiple item at a time by extend() method
final_bazar_list.extend(['ada', 'lebu'])
print(final_bazar_list)
print('\n \n \n')

# List Reversing
final_bazar_list.reverse()
print(final_bazar_list)
print('\n \n \n')

# List Soring
final_bazar_list.sort()
print(final_bazar_list)
print('\n \n \n')

# We can also define list into list or multidimensional list
multi_list = [['a', 'b', 'c'], [1, 2, 3, 4], 'x', 'y', 'z']
print(multi_list)
print(len(multi_list))

# Access multidimensional list
print(multi_list[0])
print(multi_list[0][2])

print(multi_list[1])
print(multi_list[1][3])

print(multi_list[4])

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

New tech. Real solutions. See what’s possible on Industries LIVE! with AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay