Python Data Structures
|
The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way. Data structures are “containers” that organize and group data according to type.
|
|
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
|
|
Print the second item of the list:
|
print(thislist[1])
|
|
Print the last item of the list:
|
print(thislist[-1])
|
|
A range of indexes by specifying where to start and where to end the range.
|
print(thislist[2:5])
|
|
Returns the items from the beginning to, but NOT including, "kiwi"
|
print(thislist[:4])
|
|
Returns the items from "cherry" to the end
|
print(thislist[2:])
|
|
Returns the items from "orange" (-4) to, but NOT including "mango" (-1)
|
print(thislist[-4:-1])
|
|
Check if "apple" is present in the list
|
if "apple" in thislist: print("Yes, 'apple' is in the fruits list")
|
|
append()
|
Adds an element at the end of the list
|
list.append (element)
|
|
clear()
|
Removes all the elements from the list
|
list.clear()
|
|
copy()
|
Returns a copy of the list
|
list.copy()
|
|
count()
|
Returns the number of elements with the specified value
|
List.count(element)
|
|
extend()
|
Add the elements of a list (or any iterable), to the end of the current list
|
List1.extend(List2)
|
|
index()
|
Returns the index of the first element with the specified value
|
List.index(element[,start[,end]])
|
|
insert()
|
Adds an element at the specified position
|
list.insert(<position, element)
|
|
pop()
|
Removes the element at the specified position
|
list.pop([index])
|
|
remove()
|
Removes the first item with the specified value
|
list.remove(element)
|
|
reverse()
|
Reverses the order of the list
|
list_name.reverse()
|
|
sort()
|
Sorts the list
|
List_name.sort()
|
|
del()
|
Element to be deleted is mentioned using list name and index.
|
del list.[index]
|
|
Containing only the fruits with the letter "a" in the name.
|
newlist = [x for x in fruits if "a" in x]
|
|
Only accept items that are not "apple":
|
newlist = [x for x in fruits if x != "apple"]
|
|
Accept only numbers lower than 5:
|
newlist = [x for x in range(10) if x < 5]
|
|
Set the values in the new list to upper case:
|
newlist = [x.upper() for x in fruits]
|
|
Set all values in the new list to 'hello':
|
newlist = ['hello' for x in fruits]
|
|
Return "orange" instead of "banana":
|
newlist = [x if x != "banana" else "orange" for x in fruits]
|
|
A tuple is a collection of objects which ordered and immutable. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
|
|
count()
|
Returns the number of times a specified value occurs in a tuple
|
|
index()
|
Searches the tuple for a specified value and returns the position of where it was found
|
|
remove()
|
thisset.remove("banana")
|
|
discard()
|
thisset.discard("banana")
|
|
clear()
|
method empties the set
|
|
del keyword
|
will delete the set completely(del thisset)
|
|
add()
|
Adds an element to the set
|
|
clear()
|
Removes all the elements from the set
|
|
copy()
|
Returns a copy of the set
|
|
difference()
|
Returns a set containing the difference between two or more sets
|
|
difference_update()
|
Removes the items in this set that are also included in another, specified set
|
|
discard()
|
Remove the specified item
|
|
intersection()
|
Returns a set, that is the intersection of two other sets
|
|
intersection_update()
|
Removes the items in this set that are not present in other, specified set(s)
|
|
isdisjoint()
|
Returns whether two sets have a intersection or not
|
|
issubset()
|
Returns whether another set contains this set or not
|
|
issuperset()
|
Returns whether this set contains another set or not
|
|
pop()
|
Removes an element from the set
|
|
remove()
|
Removes the specified element
|
|
symmetric_difference()
|
Returns a set with the symmetric differences of two sets
|
|
symmetric_difference_update()
|
inserts the symmetric differences from this set and another
|
|
union()
|
Return a set containing the union of sets
|
|
update()
|
Update the set with the union of this set and others
|
|
The pop() method removes the item with the specified key name:
|
thisdict.pop("model")
|
|
The popitem() method removes the last inserted item
|
thisdict.popitem()
|
|
The del keyword removes the item with the specified key name:
|
del thisdict["model"]
|
|
The del keyword can also delete the dictionary completely:
|
del thisdict
|
|
The clear() method empties the dictionary
|
thisdict.clear()
|
|
clear()
|
Removes all the elements from the dictionary
|
dictionary.clear()
|
|
copy()
|
Returns a copy of the dictionary
|
dictionary.copy()
|
|
fromkeys()
|
Returns a dictionary with the specified keys and value
|
dict.fromkeys(keys, value)
|
|
get()
|
Returns the value of the specified key
|
dictionary.get(keyname, value)
|
|
items()
|
Returns a list containing a tuple for each key value pair
|
dictionary.items()
|
|
keys()
|
Returns a list containing the dictionary's keys
|
dictionary.keys()
|
|
pop()
|
Removes the element with the specified key
|
dictionary.pop(keyname, defaultvalue)
|
|
popitem()
|
Removes the last inserted key-value pair
|
dictionary.popitem()
|
|
setdefault()
|
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
|
dictionary.setdefault(keyname, value)
|
|
update()
|
Updates the dictionary with the specified key-value pairs
|
dictionary.update(iterable)
|
|
values()
|
Returns a list of all the values in the dictionary
|
dictionary.values()
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.