Afiz ⚡️
Afiz ⚡️

@itsafiz

16 Tweets Feb 03, 2023
Lists in Python (with code snippets)
List is a frequently used data structure in Python.
In this thread, we will learn how to create and use lists in Python programming.
A Thread 🧵👇
What is List in Python?
List is an ordered sequence of items. We can store more than one item of different types in a list. Items in the list are accessed using the index
If you are coming from C/C++ or Java background, you can relate it with Array, but it has more advantages.
Make a List
In Python, a List is created by placing items inside square brackets [] and separated by commas.
Access items from the list
using index items can be accessed from the list. index starts from 0 and ends with length -1
Python supports negative indexing as well. -1 for the last item and so on.
Get the index of the item
index method is used to get the index of the item
Add items to list
There are 3 methods to add items to list
1. append
2. extend
3. insert
append adds the elements at the end as a single item
Extend method
extend method adds all the elements of an iterable(string, list, tuple) to the end of the list
insert method
insert method inserts the item/items at the given index.
Remove items from the list
remove method is used to delete the items from the list
The Count method
The count() method returns the number of times the given element appears in the list
The pop method
pop() method deletes the element from the given index
sorting list items
sort method is used to sort the elements in the list
reverse the items on the list
reverse method is used for it.
Delete all the items
clear() method is used to delete all the items from the list
Find the full code of this thread 👇
github.com
Star the repo if you like the content. ⭐️
That's a wrap!
I hope you like this content.
Follow me @itsafiz.
Like and RT the first tweet of this thread for a better reach.
Cheers 🎉

Loading suggestions...