Show Menu
Cheatography

Python3 Lists: Everything you need to know Cheat Sheet by

Python3 List Operations and functions

Summing and multip­lying

nums = [1, 2, 3]
print(nums + [4, 5, 6])
print(nums * 3)
Lists can be added and multiplied in the same way as strings.

"­ins­ert­" FUNCTION

words = ["Python", "fun"]
index = 1
words.insert(index, "is")
print(words)
----------------
>>>
['Python', 'is', 'fun']
>>>
insert method is similar to append, except that it allows you to insert a new item at any position in the list, as opposed to just at the end.

"­ran­ge" FUNCTION

numbers = list(range(5, 20, 2))
print(numbers)
-------------------------
>>>
[5, 7, 9, 11, 13, 15, 17, 19]
>>>
* The range function creates a sequential list of numbers.
*If range is called with one argument, it produces an object with values from 0 to that argument.
If it is called with two arguments, it produces values from the first to the second.
*range can have a third argument, which determines the interval of the sequence produced

ALL & ANY

nums = [55, 44, 33, 22, 11]

if all([i > 5 for i in nums]):
   print("All larger than 5")

if any([i % 2 == 0 for i in nums]):
   print("At least one is even")
Often used in condit­ional statem­ents, all and any take a list as an argument, and return True if all or any (respe­cti­vely) of their arguments evaluate to True (and False otherw­ise).
 

IN and NOT operator

words = ["spam", "egg", "spam", "sausage"]
print("spam" in words) #RETURNS TRUE
------------------------------------------
nums = [1, 2, 3]
print(not 4 in nums) #RETURNS TRUE
print(4 not in nums)
The in operator is also used to determine whether or not a string is a substring of another string.

"­ind­ex" FUNCTION

letters = ['p', 'q', 'r', 's', 'p', 'u']
print(letters.index('r'))
print(letters.index('z'))
----------------------------------
>>>
2
ValueError: 'z' is not in list
>>>
index method finds the first occurrence of a list item and returns its index.

List compre­hen­sions

cubes = [i**3 for i in range(5)]
print(cubes)
>>>
[0, 1, 8, 27, 64]
>>>

A list comprehension can also contain an if statement to enforce a condition on values in the list.
evens=[i2 for i in range(10) if i2 % 2 == 0]

print(evens)
>>>
[0, 4, 16, 36, 64]
>>>
Trying to create a list in a very extensive range will result in a Memory­Error.

ENUMERATE

nums = [55, 44, 33, 22, 11]

for v in enumerate(nums):
   print(v)

-------------------------
(0, 55)
(1, 44)
(2, 33)
(3, 22)
(4, 11)
The function enumerate can be used to iterate through the values and indices of a list simult­ane­ously.
 

"­app­end­" FUNCTION

nums = [1, 2, 3]
nums.append(4)
print(nums)
------------------------
>>>
[1, 2, 3, 4]
>>>
This adds an item to the end of an existing list.

"­Len­" FUNCTION

nums = [1, 3, 5, 2, 4]
print(len(nums))
-------------------------------
>>>
5
>>>

List slicing 1

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(squares[2:6])
print(squares[3:8])
------------
[4, 9, 16, 25]
[9, 16, 25, 36, 49]
Basic list slicing involves indexing a list with two colon-­sep­arated integers.

List slicing 2

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(squares[::2])
print(squares[2:8:3]).
...................
>>>
[0, 4, 16, 36, 64]
[4, 25]
>>>
....................
Negative values can be used in list slicing (and normal list indexing). When negative values are used for the first and second values in a slice (or a normal index), they count from the end of the list.
squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(squares[1:-1])
>>>
[1, 4, 9, 16, 25, 36, 49, 64]
>>>
If a negative value is used for the step, the slice is done backwards.
Using [::-1] as a slice is a common and idiomatic way to reverse a list.
List slices can also have a third number, repres­enting the step, to include only alternate values in the slice.
               
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by nimakarimian

          C++ Pointers cookbook Cheat Sheet
          Dart numbers Cheat Sheet