Understanding Lists in Python- Wakeupcoders
In Python, multiple elements can be kept in a single variable by using lists. One of the four built-in data types in Python for storing data collections is the list. The other three are the tuple, set, and dictionary, each of which has a unique purpose. Let’s move to understand the lists in python
List the type of Data type in Python
example:
fruits = ["strawberry", "mango", "date"]
print(fruits)
What exactly List is?
List items can have duplicate values and are sorted and editable. The first item in a list has the index [0], the second item has the index [1], etc.
When we refer to a list as being sorted, we indicate that the entries are in a specific order that will not alter. The new things will be added at the end of the list if you add more items. The list is modifiable, which means that after it has been generated, we may edit, add, and delete entries from it.
Functions in List
Access Items
List items can be accessed by using the index number, which is indexed:
fruits = ["strawberry", "mango", "date"]
print(fruits[1])
Change List items
If you want to update the value of a certain item, look up the index number:
fruits = ["strawberry", "mango", "date"]
fruits[1] = "apple"
print(fruits)
Add items
Use the append() function to add an item to the list’s end:
fruits = ["strawberry", "mango", "date"]
fruits.append("apple")
print(fruits)
Remove List items
The supplied item is removed using the remove() function.
fruits = ["strawberry", "mango", "date"]
fruits.remove("mango")
print(fruits)
Loop Lists
Using a for loop, you may cycle through the entries on the list:
fruits = ["strawberry", "mango", "date"]
for x in fruits:
print(x)
Join Lists
To join the two lists we use + operator
aplha = ["a", "b", "c"]
numeric = [1, 2, 3]
final = aplha + numeric
print(final)
Let’s Wind up:
If you liked this Blog and found this blog informative do share with your friends and co-workers to help them. Till then keep Reading and Enjoying.
If you are having any feedback or looking a way to connect with us.
Let’s connect together www.wakeupcoders.com/contact & think beyond everything.
Thanks to the Wakeupcoders Team: For thinking beyond everything
Purnima Sinha (author), Narayan Jha (editor), Anjali Jha (digital marketing)