Python Tutorial: Lists Explained with Examples

In Python, a list is a collection of items that are ordered and changeable. Lists are written with square brackets [] and can contain any type of data, including other lists. Here are a few examples of creating and manipulating lists:

Python Tutorial Lists Explained with Examples

Example 1: Creating a list

# Creating a list of integers
numbers = [1, 2, 3, 4, 5]

# Creating a list of strings
animals = ["dog", "cat", "bird"]

# Creating a list of mixed data types
mixed = [1, "dog", 3.14, True]

Example 2: Accessing items in a list

# Accessing the first item in a list
first_item = numbers[0] # 1
# Accessing the last item in a list
last_item = numbers[-1] # 5

Example 3: Modifying a list

# Changing an item in a list
numbers[1] = 10
print(numbers) # [1, 10, 3, 4, 5]

# Adding an item to a list
numbers.append(6)
print(numbers) # [1, 10, 3, 4, 5, 6]

# Removing an item from a list
numbers.remove(4)
print(numbers) # [1, 10, 3, 5, 6]


Example 4: Iterating over a list

# Using a for loop to print each item in a list
for animal in animals:
    print(animal)
# Output:
# dog
# cat
# bird

Example 5: Using built-in functions

# Using the len() function to get the number of items in a list
count = len(animals) # 3

# Using the min() and max() functions to get the smallest and largest items in a list
smallest = min(numbers) # 1
largest = max(numbers) # 10

# Using the sum() function to get the total of items in a list
total = sum(numbers) # 30

Example 6: Slicing a list

# Slicing a list to get a sub-list
sub_list = numbers[1:4] # [10, 3, 5]

# Slicing a list with step
sub_list = numbers[::2] # [1, 3, 5]

Example 7: List comprehension

# Using list comprehension to create a new list
squares = [x*x for x in range(1,6)] # [1, 4, 9, 16, 25]

# Using list comprehension with condition
even_numbers = [x for x in numbers if x%2 == 0] # [10]

Example 8: List methods

# Using the extend() method to add multiple items to a list
numbers.extend([7, 8, 9])
print(numbers) # [1, 10, 3, 5, 6, 7, 8, 9]

# Using the insert() method to add an item to a specific index in a list
numbers.insert(3, 4)
print(numbers) # [1, 10, 3, 4, 5, 6, 7, 8, 9]

# Using the index() method to find the index of an item in a list
index = numbers.index(10) # 1

# Using the count() method to count the number of occurrences of an item in a list
count = numbers.count(4) # 1

Example 9: Using the zip function

# Using zip function to join multiple lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
zipped = list(zip(list1, list2))
print(zipped) # [(1, 4), (2, 5), (3, 6)]

These examples demonstrate some of the different ways to manipulate lists in Python, including slicing, list comprehension, built-in list methods, and using the zip function to join multiple lists together.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is HBr polar or nonpolar Is HCl polar or nonpolar Is NO2+ Polar or Nonpolar Is H2S Polar or Nonpolar Is PCl3 Polar or Nonpolar