#python #programming #question #intermediate #listoperations
Write a comprehensive Python program that demonstrates various ways to work with lists in different scenarios:
1. Create a list of integers from 1 to 10 using the range() function and convert it to a list.
2. Demonstrate list comprehension by creating a new list containing squares of even numbers from the original list.
3. Use list slicing to extract sublists (first 5 elements, last 3 elements, every second element).
4. Show how to modify list elements by changing specific indices and using negative indexing.
5. Implement a nested list and demonstrate accessing elements at different levels.
6. Use the enumerate() function to iterate through a list while tracking index and value.
7. Create two lists and use zip() to combine them into pairs.
8. Perform list operations: append, extend, insert, remove, pop, and sort.
9. Use the count() and index() methods to find occurrences and positions of elements.
10. Create a list of strings and use the join() method to concatenate them with a separator.
By: @DataScienceQ 🚀
  Write a comprehensive Python program that demonstrates various ways to work with lists in different scenarios:
1. Create a list of integers from 1 to 10 using the range() function and convert it to a list.
2. Demonstrate list comprehension by creating a new list containing squares of even numbers from the original list.
3. Use list slicing to extract sublists (first 5 elements, last 3 elements, every second element).
4. Show how to modify list elements by changing specific indices and using negative indexing.
5. Implement a nested list and demonstrate accessing elements at different levels.
6. Use the enumerate() function to iterate through a list while tracking index and value.
7. Create two lists and use zip() to combine them into pairs.
8. Perform list operations: append, extend, insert, remove, pop, and sort.
9. Use the count() and index() methods to find occurrences and positions of elements.
10. Create a list of strings and use the join() method to concatenate them with a separator.
# 1. Create a list of integers from 1 to 10
numbers = list(range(1, 11))
print("Original list:", numbers)
# 2. List comprehension - squares of even numbers
squares_of_evens = [x**2 for x in numbers if x % 2 == 0]
print("Squares of even numbers:", squares_of_evens)
# 3. List slicing
print("\nList slicing examples:")
print("First 5 elements:", numbers[:5])
print("Last 3 elements:", numbers[-3:])
print("Every second element:", numbers[::2])
# 4. Modifying list elements
print("\nModifying list elements:")
numbers[0] = 100 # Change first element
numbers[-1] = 200 # Change last element
print("After modifications:", numbers)
# 5. Nested lists
print("\nNested lists:")
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Accessing nested elements:")
print("First row:", nested_list[0])
print("Element at position [1][2]:", nested_list[1][2])
# 6. Using enumerate()
print("\nUsing enumerate():")
for index, value in enumerate(numbers):
print(f"Index {index}: {value}")
# 7. Using zip() with two lists
print("\nUsing zip() with two lists:")
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 8. List operations
print("\nList operations:")
fruits = ['apple', 'banana']
print("Initial fruits:", fruits)
# Append
fruits.append('orange')
print("After append:", fruits)
# Extend
fruits.extend(['grape', 'kiwi'])
print("After extend:", fruits)
# Insert
fruits.insert(1, 'mango')
print("After insert:", fruits)
# Remove
fruits.remove('grape')
print("After remove:", fruits)
# Pop
popped_item = fruits.pop()
print(f"Popped item: {popped_item}, Remaining: {fruits}")
# Sort
fruits.sort()
print("After sorting:", fruits)
# 9. Using count() and index()
print("\nUsing count() and index():")
numbers_with_duplicates = [1, 2, 2, 3, 2, 4]
print("Numbers with duplicates:", numbers_with_duplicates)
print("Count of 2:", numbers_with_duplicates.count(2))
print("Index of first 2:", numbers_with_duplicates.index(2))
# 10. Joining strings in a list
print("\nJoining strings:")
words = ['Hello', 'world', 'from', 'Python']
sentence = ' '.join(words)
print("Joined sentence:", sentence)
# Additional example: list of dictionaries
print("\nList of dictionaries:")
students = [
{'name': 'Alice', 'grade': 85},
{'name': 'Bob', 'grade': 92},
{'name': 'Charlie', 'grade': 78}
]
print("Students data:")
for student in students:
print(f"{student['name']}: {student['grade']}")
# Example: filtering with list comprehension
print("\nFiltering students with grade > 80:")
high_performers = [student for student in students if student['grade'] > 80]
for student in high_performers:
print(f"{student['name']}: {student['grade']}")
By: @DataScienceQ 🚀