-3

I want to sort a list, wich I do with the .sort or sorted command. Afterwards i want to change the Numbers into words so it makes more sense to read.

My Problem:

Calculation1 = 0+1   #because i need to cacluate something first
calculation2 = 1+1

list1 = [1, 2]

list1.sort()

output: 2, 1

but I want to change 2, 1 so it says FirstInvestment, SecondInvestment

thx for the help and constructive criticism

MrrLIme
  • 7
  • 3
  • 2
    Does this answer your question? [What is the difference between \`sorted(list)\` vs \`list.sort()\`?](https://stackoverflow.com/questions/22442378/what-is-the-difference-between-sortedlist-vs-list-sort) – Alex Waygood Sep 10 '21 at 21:17
  • 4
    What are you *really* trying to achieve? [XY problem](https://xyproblem.info/) – no comment Sep 10 '21 at 21:18
  • Do you want to sort by the text names or the values? – tdelaney Sep 10 '21 at 21:33
  • I want to sort by the Values – MrrLIme Sep 10 '21 at 21:37
  • "but how do i change the variable without making a new list and assign the variables t1 and t2 new like:" Why does it matter? Why not just `List1 = sorted(List1)`? in any case, you can always use the `.sort` method on `list` objects if you want to sort them in-place – juanpa.arrivillaga Sep 10 '21 at 21:39
  • 2
    "because if i do it like this i would "unsort" the whole thing" What??? – juanpa.arrivillaga Sep 10 '21 at 21:39
  • 1
    @MrrLIme Ah, I see. You are fundamentally confused, *variables are not strings*. Objects have no idea what variables might refer to them, and in general, **should never need to care**. If you want numbers associated with strings, then you need to create a data structure that does that (a dict, a list of tuples, etc). Fundamentally, you need to understand that *variables are for source code*, their names shouldn't contain program data. – juanpa.arrivillaga Sep 10 '21 at 21:41
  • 2
    The reason your description is confusing is because `[big, small] ` **doesn't have any text**. `big` and `small` are *variables* But note, list objects, or any other object, *don't contain or refer to variables*. They contain or refer to *other objects*. Again, *variables are for source code*. Their for the *human reading the code*. – juanpa.arrivillaga Sep 10 '21 at 21:42

1 Answers1

1

You're calling a method that returns a new list. So what's happening is you call sorted() on List1 and sorted is returning a new list for you (which you don't use). Then you print out the original list.

You should use the in-place sort on the list object.

List1 = [t1, t2]
List1.sort()
print(List1)
Enigma
  • 373
  • 2
  • 10