nathanwailes

Lists

Jun 9th, 2024 (edited)
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. """
  2. TODO: I need some way to verify that I remember the important operations. Right now I can remember a handful of the things off the top of my head but I inevitably forget something. I think the ideal would be to have some kind of random quiz that asks you to accomplish certain things, and it makes sure it tests for every thing you can do with lists.
  3. """
  4.  
  5.  
  6. # Create a list
  7. my_list = [1, 2, 3, 4, 5]
  8.  
  9. # Access elements
  10. first_element = my_list[0]         # Access the first element
  11. last_element = my_list[-1]         # Access the last element
  12.  
  13. # Modify elements
  14. my_list[2] = 10                    # Modify the third element
  15.  
  16. # Add elements
  17. my_list.append(6)                  # Add an element to the end
  18. my_list.insert(2, 7)               # Insert an element at a specific position
  19.  
  20. # Remove elements
  21. my_list.remove(4)                  # Remove the first occurrence of an element
  22. removed_element = my_list.pop()    # Remove and return the last element
  23. second_element = my_list.pop(1)    # Remove and return the second element
  24.  
  25. # List length
  26. length = len(my_list)              # Get the number of elements in the list
  27.  
  28. # Slicing
  29. sub_list = my_list[1:4]            # Get a sublist (from index 1 to 3)
  30. every_other_element = my_list[::2] # Get every other element
  31.  
  32. # List concatenation
  33. another_list = [8, 9]
  34. concatenated_list = my_list + another_list  # Concatenate two lists
  35.  
  36. # List repetition
  37. repeated_list = my_list * 2         # Repeat the list
  38.  
  39. # Check for element existence
  40. is_in_list = 7 in my_list           # Check if an element exists in the list
  41.  
  42. # List comprehension
  43. squares = [x**2 for x in my_list]   # Create a new list with the squares of the elements
  44.  
  45. # Iterating through a list
  46. for element in my_list:
  47.     print(element)                 # Print each element
  48.  
  49. # List methods
  50. my_list.sort()                     # Sort the list in ascending order
  51. my_list.reverse()                  # Reverse the list
  52. my_list.clear()                    # Remove all elements from the list
  53.  
  54. # List copying
  55. copied_list = my_list.copy()       # Create a shallow copy of the list
  56.  
  57. # Nested lists
  58. nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  59. first_sub_list = nested_list[0]    # Access the first sublist
  60. element_in_nested_list = nested_list[1][2]  # Access an element in a sublist
  61.  
  62. # List conversion
  63. string_list = "1,2,3,4,5"
  64. converted_list = string_list.split(",")   # Convert a string to a list
  65. back_to_string = ",".join(converted_list) # Convert a list to a string
  66.  
Advertisement
Add Comment
Please, Sign In to add comment