Skip to content

Tag: list

Combine two lists and sort them

Lets say I have two lists. I want to append list2 into list1 and then sort and add a new element at a specific index. I keep getting an error message saying: TypeError: ‘<‘ not supported between instances of ‘list’ and ‘int’ This is what I have tried: Answer Use sorted()…

Sorting list based on dictionary keys in python

Is there a short way to sort a list based on the order of another dictionary keys? suppose I have: I want to sort the list to be [‘a’,’b’,’c’] based on the order of dic keys. Answer You can create a lookup of keys versus their insertion order in dic. To do so you can write:…

Check a list contains given elements

I want to check if my list’s first four elements are digits. what i did is as follows: But this gives the following error. How can I achieve this? Answer The error is telling you that you can’t do some_list in some_string – after all, a list consists of characters, not lists, so it’s p…