The Art of Writing Short Stories Read Here

Python Lists and Tuples Operations Cheat Sheet

Lists and Tuples Operations Cheat Sheet

Lists and tuples are both sequences, so they share a number of sequence operations. But, because lists are mutable, there are also a number of methods specific just to lists. This cheat sheet gives you a run down of the common operations first, and the list-specific operations second.

Common sequence operations

  • len(sequence) Returns the length of the sequence
  • for element in sequence Iterates over each element in the sequence
  • if element in sequence Checks whether the element is part of the sequence
  • sequence[i] Accesses the element at index i of the sequence, starting at zero
  • sequence[i:j] Accesses a slice starting at index i, ending at index j-1. If i is omitted, it's 0 by default. If j is omitted, it's len(sequence) by default.
  • for index, element in enumerate(sequence) Iterates over both the indexes and the elements in the sequence at the same time

Check out the official documentation for sequence operations.

List-specific operations and methods

  • list[i] = x Replaces the element at index i with x
  • list.append(x) Inserts x at the end of the list
  • list.insert(i, x) Inserts x at index i
  • list.pop(i) Returns the element a index i, also removing it from the list. If i is omitted, the last element is returned and removed.
  • list.remove(x) Removes the first occurrence of x in the list
  • list.sort() Sorts the items in the list
  • list.reverse() Reverses the order of items of the list
  • list.clear() Removes all the items of the list
  • list.copy() Creates a copy of the list
  • list.extend(other_list) Appends all the elements of other_list at the end of list

Most of these methods come from the fact that lists are mutable sequences. For more info, see the official documentation for mutable sequences and the list specific documentation.

List comprehension

  • [expression for the variable in sequence] Creates a new list based on the given sequence. Each element is the result of the given expression.
  • [expression for the variable in sequence if condition] Creates a new list based on the given sequence. Each element is the result of the given expression; elements only get added if the condition is true.
  1. list and tuples in python ppt
  2. list and tuples in python pdf
  3. list and tuples in python w3schools
  4. difference between lists and tuples python
  5. lists vs tuples python
  6. concatenate list and tuple python
  7. list and tuple in python program
  8. merge list and tuple python
  9. list of tuples python access
  10. list of tuples python append
  11. list tuple array python
  12. a list of tuples python
  13. list of tuples python create
  14. list of tuples python count
  15. list of tuples comprehension python
  16. list to tuple conversion python
  17. list to tuple converter python
  18. lists and tuples in python
  19. list and tuple difference python
  20. lists tuples dictionaries python
  21. list tuple dict python
  22. list tuple dictionary python ppt
  23. list of tuples python example
  24. python list and tuple exercises
  25. list of tuples element python
  26. list of tuples python for loop
  27. list of tuples python get first
  28. list of tuples groupby python
  29. given that python lists and python tuples are quite similar
  30. list and tuple in python in hindi
  31. list and tuple in python difference
  32. list dictionaries and tuples in python
  33. compare list and tuples in python
  34. list tuple join python
  35. list and tuples in python
  36. lists dictionaries and tuples in python
  37. list tuple and set in python
  38. list and tuples
  39. what are lists and tuples in python
  40. what is list and tuples in python
  41. list tuples and dictionary in python ppt
  42. list vs tuple performance python
  43. list of tuple pairs python
  44. python lists and tuples quiz
  45. python list and tuple questions
  46. list tuple range python
  47. list tuple python sorting
  48. list tuples sets python
  49. list of tuples python split
  50. list of tuples python string
  51. list of tuples search python
  52. list of tuples python to dict
  53. list tuple to python
  54. python lists and tuples
  55. python programs on lists and tuples
  56. programs on list and tuples in python
  57. python list vs tuple
  58. two lists into tuples python
  59. list of tuples python 3
  60. list vs tuple python 3


 

You may also like :