arrange list python code example

Example 1: python sort list

# sort() will change the original list into a sorted list
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
# Output:
# ['a', 'e', 'i', 'o', 'u']

# sorted() will sort the list and return it while keeping the original
sortedVowels = sorted(vowels)
# Output:
# ['a', 'e', 'i', 'o', 'u']

Example 2: python add to list

list_to_add.append(item_to_add)
# or
list_to_add.push(item_to_add)

Example 3: how to manually sort a list in python

Numbers = []
iterate = 0

while len(Numbers)<5:
    try:
        x = int(input("Insert the number you want in the list: "))
        Numbers.append(x)
    except:
        print("The input  MUST be a number.")
        continue
    for iteration_count in range(len(Numbers)):
        #This acts like a counting method
        for j in range(0,len(Numbers)-1):
            #It swaps each element for each iteration (j is just a random variable)
            if (Numbers[j]>Numbers[j+1]):
                Numbers[j],Numbers[j+1] = Numbers[j+1],Numbers[j]
print(f"Here are the sorted numbers:{Numbers}")

Example 4: sorting python array

sorted(list, key=..., reverse=...)