Sitemap
Python Journal

Learn with Python Devs.

Member-only story

Master Python Lists: Tips, Tricks, and Best Practices

2 min readAug 29, 2024

--

Press enter or click to view image in full size

What is list in Python?

List: A list is a collection of items that are ordered and mutable.

_list = list()
fruits = ['apple', 'banana', 'cherry']

# Nested lists
nested_list = [['apple', 'red'], ['banana', 'yellow']]

Inserting elements:

fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "kiwi") # Inserts "kiwi" at index 1
print(fruits) # Output: ['apple', "kiwi", 'banana', 'cherry']

List unpacking with the use of the splat(*) operator:

fruits = ["apple", "banana", "cherry", "strawberry"]
green, yellow, *red = fruits
# green = apple, yellow = banana, red = ['cherry', 'strawberry']

fruits = ["apple", "banana", "cherry", "strawberry"]
*green, yellow = fruits
# green = ['apple', 'banana', 'cherry'], yellow = ['strawberry']

Using zip with Lists:

fruits = ["apple", "banana", "strawberry"]
color = ["red", "yellow", "pink"]
combined =…

--

--

Tushar Bosamiya
Tushar Bosamiya

Written by Tushar Bosamiya

Self-taught Dev. sharing insights on software development, Python, Data Engineering, AWS, GCP and Azure. Follow for innovative and valuable software insights!