DATASCIENCE4 Telegram 8545
💡 Python Lists Cheatsheet: Essential Operations

This lesson provides a quick reference for common Python list operations. Lists are ordered, mutable collections of items, and mastering their use is fundamental for Python programming. This cheatsheet covers creation, access, modification, and utility methods.

# 1. List Creation
my_list = [1, "hello", 3.14, True]
empty_list = []
numbers = list(range(5)) # [0, 1, 2, 3, 4]

# 2. Accessing Elements (Indexing & Slicing)
first_element = my_list[0] # 1
last_element = my_list[-1] # True
sub_list = my_list[1:3] # ["hello", 3.14]
copy_all = my_list[:] # [1, "hello", 3.14, True]

# 3. Modifying Elements
my_list[1] = "world" # my_list is now [1, "world", 3.14, True]

# 4. Adding Elements
my_list.append(False) # [1, "world", 3.14, True, False]
my_list.insert(1, "new item") # [1, "new item", "world", 3.14, True, False]
another_list = [5, 6]
my_list.extend(another_list) # [1, "new item", "world", 3.14, True, False, 5, 6]

# 5. Removing Elements
removed_value = my_list.pop() # Removes and returns last item (6)
removed_at_index = my_list.pop(1) # Removes and returns "new item"
my_list.remove("world") # Removes the first occurrence of "world"
del my_list[0] # Deletes item at index 0 (1)
my_list.clear() # Removes all items, list becomes []

# Re-create for other examples
numbers = [3, 1, 4, 1, 5, 9, 2]

# 6. List Information
list_length = len(numbers) # 7
count_ones = numbers.count(1) # 2
index_of_five = numbers.index(5) # 4 (first occurrence)
is_present = 9 in numbers # True
is_not_present = 10 not in numbers # True

# 7. Sorting
numbers_sorted_asc = sorted(numbers) # Returns new list: [1, 1, 2, 3, 4, 5, 9]
numbers.sort(reverse=True) # Sorts in-place: [9, 5, 4, 3, 2, 1, 1]

# 8. Reversing
numbers.reverse() # Reverses in-place: [1, 1, 2, 3, 4, 5, 9]

# 9. Iteration
for item in numbers:
# print(item)
pass # Placeholder for loop body

# 10. List Comprehensions (Concise creation/transformation)
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
even_numbers = [x for x in numbers if x % 2 == 0] # [2, 4]


Code explanation: This script demonstrates fundamental list operations in Python. It covers creating lists, accessing elements using indexing and slicing, modifying existing elements, adding new items with append(), insert(), and extend(), and removing items using pop(), remove(), del, and clear(). It also shows how to get list information like length (len()), item counts (count()), and indices (index()), check for item existence (in), sort (sort(), sorted()), reverse (reverse()), and iterate through lists. Finally, it illustrates list comprehensions for concise list generation and filtering.

#Python #Lists #DataStructures #Programming #Cheatsheet

━━━━━━━━━━━━━━━
By: @DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
2



tgoop.com/DataScience4/8545
Create:
Last Update:

💡 Python Lists Cheatsheet: Essential Operations

This lesson provides a quick reference for common Python list operations. Lists are ordered, mutable collections of items, and mastering their use is fundamental for Python programming. This cheatsheet covers creation, access, modification, and utility methods.

# 1. List Creation
my_list = [1, "hello", 3.14, True]
empty_list = []
numbers = list(range(5)) # [0, 1, 2, 3, 4]

# 2. Accessing Elements (Indexing & Slicing)
first_element = my_list[0] # 1
last_element = my_list[-1] # True
sub_list = my_list[1:3] # ["hello", 3.14]
copy_all = my_list[:] # [1, "hello", 3.14, True]

# 3. Modifying Elements
my_list[1] = "world" # my_list is now [1, "world", 3.14, True]

# 4. Adding Elements
my_list.append(False) # [1, "world", 3.14, True, False]
my_list.insert(1, "new item") # [1, "new item", "world", 3.14, True, False]
another_list = [5, 6]
my_list.extend(another_list) # [1, "new item", "world", 3.14, True, False, 5, 6]

# 5. Removing Elements
removed_value = my_list.pop() # Removes and returns last item (6)
removed_at_index = my_list.pop(1) # Removes and returns "new item"
my_list.remove("world") # Removes the first occurrence of "world"
del my_list[0] # Deletes item at index 0 (1)
my_list.clear() # Removes all items, list becomes []

# Re-create for other examples
numbers = [3, 1, 4, 1, 5, 9, 2]

# 6. List Information
list_length = len(numbers) # 7
count_ones = numbers.count(1) # 2
index_of_five = numbers.index(5) # 4 (first occurrence)
is_present = 9 in numbers # True
is_not_present = 10 not in numbers # True

# 7. Sorting
numbers_sorted_asc = sorted(numbers) # Returns new list: [1, 1, 2, 3, 4, 5, 9]
numbers.sort(reverse=True) # Sorts in-place: [9, 5, 4, 3, 2, 1, 1]

# 8. Reversing
numbers.reverse() # Reverses in-place: [1, 1, 2, 3, 4, 5, 9]

# 9. Iteration
for item in numbers:
# print(item)
pass # Placeholder for loop body

# 10. List Comprehensions (Concise creation/transformation)
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
even_numbers = [x for x in numbers if x % 2 == 0] # [2, 4]


Code explanation: This script demonstrates fundamental list operations in Python. It covers creating lists, accessing elements using indexing and slicing, modifying existing elements, adding new items with append(), insert(), and extend(), and removing items using pop(), remove(), del, and clear(). It also shows how to get list information like length (len()), item counts (count()), and indices (index()), check for item existence (in), sort (sort(), sorted()), reverse (reverse()), and iterate through lists. Finally, it illustrates list comprehensions for concise list generation and filtering.

#Python #Lists #DataStructures #Programming #Cheatsheet

━━━━━━━━━━━━━━━
By: @DataScience4

BY Python | Algorithms | Data Structures | Cyber ​​Security | Networks


Share with your friend now:
tgoop.com/DataScience4/8545

View MORE
Open in Telegram


Telegram News

Date: |

Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. How to create a business channel on Telegram? (Tutorial) Ng, who had pleaded not guilty to all charges, had been detained for more than 20 months. His channel was said to have contained around 120 messages and photos that incited others to vandalise pro-government shops and commit criminal damage targeting police stations. A Telegram channel is used for various purposes, from sharing helpful content to implementing a business strategy. In addition, you can use your channel to build and improve your company image, boost your sales, make profits, enhance customer loyalty, and more. The Standard Channel
from us


Telegram Python | Algorithms | Data Structures | Cyber ​​Security | Networks
FROM American