Python interview questions
152 questions with answers. 6,346 open jobs in India ask for Python.
What are the rules for local and global variables in Python?
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.
Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring
globalfor assigned variables provides a bar against unintended side-effects. On the other hand, ifglobalwas required for all global references, you’d be usingglobalall the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of theglobaldeclaration for identifying side-effects.From Python Frequently Asked Questions (PSF License Version 2)
Why do lambdas defined in a loop with different values all return the same result?
Assume you use a for loop to define a few different lambdas (or even plain functions), for example:
>>> squares = [] >>> for x in range(5): ... squares.append(lambda: x**2)
This gives you a list that contains 5 lambdas that calculate
x**2. You might expect that, when called, they would return, respectively,0,1,4,9, and16. However, when you actually try you will see that they all return16:>>> squares[2]() 16 >>> squares[4]() 16
This happens because
xis not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined. At the end of the loop, the value ofxis4, so all the functions now return4**2, that is16. You can also verify this by changing the value ofxand see how the results of the lambdas change:>>> x = 8 >>> squares[2]() 64
In order to avoid this, you need to save the values in variables local to the lambdas, so that they don’t rely on the value of the global
x:>>> squares = [] >>> for x in range(5): ... squares.append(lambda n=x: n**2)
Here,
n=xcreates a new variablenlocal to the lambda and computed when the lambda is defined so that it has the same value thatxhad at that point in the loop. This means that the value ofnwill be0in the first lambda,1in the second,2in the third, and so on. Therefore each lambda will now return the correct result:>>> squares[2]() 4 >>> squares[4]() 16
Note that this behaviour is not peculiar to lambdas, but applies to regular functions too.
From Python Frequently Asked Questions (PSF License Version 2)
How do I share global variables across modules?
The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example:
config.py:
x = 0 # Default value of the 'x' configuration setting
mod.py:
import config config.x = 1
main.py:
import config import mod print(config.x)
Note that using a module is also the basis for implementing the singleton design pattern, for the same reason.
From Python Frequently Asked Questions (PSF License Version 2)
What are the “best practices” for using import in a module?
In general, don’t use
from modulename import *. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.Import modules at the top of a file. Doing so makes it clear what other modules your code requires and avoids questions of whether the module name is in scope. Using one import per line makes it easy to add and delete module imports, but using multiple imports per line uses less screen space.
It’s good practice if you import modules in the following order:
third-party library modules (anything installed in Python’s site-packages directory) – such as dateutil, requests, tzdata
locally developed modules
It is sometimes necessary to move imports to a function or class to avoid problems with circular imports. Gordon McMillan says:
Circular imports are fine where both modules use the “import <module>” form of import. They fail when the 2nd module wants to grab a name out of the first (“from module import name”) and the import is at the top level. That’s because names in the 1st are not yet available, because the first module is busy importing the 2nd.
In this case, if the second module is only used in one function, then the import can easily be moved into that function. By the time the import is called, the first module will have finished initializing, and the second module can do its import.
It may also be necessary to move imports out of the top level of code if some of the modules are platform-specific. In that case, it may not even be possible to import all of the modules at the top of the file. In this case, importing the correct modules in the corresponding platform-specific code is a good option.
Only move imports into a local scope, such as inside a function definition, if it’s necessary to solve a problem such as avoiding a circular import or are trying to reduce the initialization time of a module. This technique is especially helpful if many of the imports are unnecessary depending on how the program executes. You may also want to move imports into a function if the modules are only ever used in that function. Note that loading a module the first time may be expensive because of the one time initialization of the module, but loading a module multiple times is virtually free, costing only a couple of dictionary lookups. Even if the module name has gone out of scope, the module is probably available in
sys.modules.From Python Frequently Asked Questions (PSF License Version 2)
Why are default values shared between objects?
This type of bug commonly bites neophyte programmers. Consider this function:
def foo(mydict={}): # Danger: shared reference to one dict for all calls ... compute something ... mydict[key] = value return mydictThe first time you call this function,
mydictcontains a single item. The second time,mydictcontains two items because whenfoo()begins executing,mydictstarts out with an item already in it.It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined. If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object.
By definition, immutable objects such as numbers, strings, tuples, and
None, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion.Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use
Noneas the default value and inside the function, check if the parameter isNoneand create a new list/dictionary/whatever if it is. For example, don’t write:def foo(mydict={}): ...but:
def foo(mydict=None): if mydict is None: mydict = {} # create a new dict for local namespaceThis feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called “memoizing”, and can be implemented like this:
# Callers can only provide two parameters and optionally pass _cache by keyword def expensive(arg1, arg2, *, _cache={}): if (arg1, arg2) in _cache: return _cache[(arg1, arg2)] # Calculate the value result = ... expensive computation ... _cache[(arg1, arg2)] = result # Store result in the cache return resultYou could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.
From Python Frequently Asked Questions (PSF License Version 2)
How can I pass optional or keyword parameters from one function to another?
Collect the arguments using the
*and**specifiers in the function’s parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using*and**:def f(x, *args, **kwargs): ... kwargs['width'] = '14.3c' ... g(x, *args, **kwargs)From Python Frequently Asked Questions (PSF License Version 2)
Why did changing list ‘y’ also change list ‘x’?
If you wrote code like:
>>> x = [] >>> y = x >>> y.append(10) >>> y [10] >>> x [10]
you might be wondering why appending an element to
ychangedxtoo.There are two factors that produce this result:
Variables are simply names that refer to objects. Doing
y = xdoesn’t create a copy of the list – it creates a new variableythat refers to the same objectxrefers to. This means that there is only one object (the list), and bothxandyrefer to it.Lists are mutable, which means that you can change their content.
After the call to
append(), the content of the mutable object has changed from[]to[10]. Since both the variables refer to the same object, using either name accesses the modified value[10].If we instead assign an immutable object to
x:>>> x = 5 # ints are immutable >>> y = x >>> x = x + 1 # 5 can't be mutated, we are creating a new object here >>> x 6 >>> y 5
we can see that in this case
xandyare not equal anymore. This is because integers are immutable, and when we dox = x + 1we are not mutating the int5by incrementing its value; instead, we are creating a new object (the int6) and assigning it tox(that is, changing which objectxrefers to). After this assignment we have two objects (the ints6and5) and two variables that refer to them (xnow refers to6butystill refers to5).Some operations (for example
y.append(10)andy.sort()) mutate the object, whereas superficially similar operations (for exampley = y + [10]andsorted(y)) create a new object. In general in Python (and in all cases in the standard library) a method that mutates an object will returnNoneto help avoid getting the two types of operations confused. So if you mistakenly writey.sort()thinking it will give you a sorted copy ofy, you’ll instead end up withNone, which will likely cause your program to generate an easily diagnosed error.However, there is one class of operations where the same operation sometimes has different behaviors with different types: the augmented assignment operators. For example,
+=mutates lists but not tuples or ints (a_list += [1, 2, 3]is equivalent toa_list.extend([1, 2, 3])and mutatesa_list, whereassome_tuple += (1, 2, 3)andsome_int += 1create new objects).In other words:
Read the rest at the sourceFrom Python Frequently Asked Questions (PSF License Version 2)
How do I write a function with output parameters (call by reference)?
Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there’s no alias between an argument name in the caller and callee, and consequently no call-by-reference. You can achieve the desired effect in a number of ways.
By returning a tuple of the results:
>>> def func1(a, b): ... a = 'new-value' # a and b are local names ... b = b + 1 # assigned to new objects ... return a, b # return new values ... >>> x, y = 'old-value', 99 >>> func1(x, y) ('new-value', 100)This is almost always the clearest solution.
By using global variables. This isn’t thread-safe, and is not recommended.
By passing a mutable (changeable in-place) object:
>>> def func2(a): ... a[0] = 'new-value' # 'a' references a mutable list ... a[1] = a[1] + 1 # changes a shared object ... >>> args = ['old-value', 99] >>> func2(args) >>> args ['new-value', 100]
By passing in a dictionary that gets mutated:
>>> def func3(args): ... args['a'] = 'new-value' # args is a mutable dictionary ... args['b'] = args['b'] + 1 # change it in-place ... >>> args = {'a': 'old-value', 'b': 99} >>> func3(args) >>> args {'a': 'new-value', 'b': 100}Or bundle up values in a class instance:
>>> class Namespace: ... def __init__(self, /, **args): ... for key, value in args.items(): ... setattr(self, key, value) ... >>> def func4(args): ... args.a = 'new-value' # args is a mutable Namespace ... args.b = args.b + 1 # change object in-place ... >>> args = Namespace(a='old-value', b=99) >>> func4(args) >>> vars(args) {'a': 'new-value', 'b': 100}There’s almost never a good reason to get this complicated.
Your best choice is to return a tuple containing the multiple results.
From Python Frequently Asked Questions (PSF License Version 2)
How do you make a higher order function in Python?
You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define
linear(a,b)which returns a functionf(x)that computes the valuea*x+b. Using nested scopes:def linear(a, b): def result(x): return a * x + b return resultOr using a callable object:
class linear: def __init__(self, a, b): self.a, self.b = a, b def __call__(self, x): return self.a * x + self.bIn both cases,
taxes = linear(0.3, 2)
gives a callable object where
taxes(10e6) == 0.3 * 10e6 + 2.The callable object approach has the disadvantage that it is a bit slower and results in slightly longer code. However, note that a collection of callables can share their signature via inheritance:
class exponential(linear): # __init__ inherited def __call__(self, x): return self.a * (x ** self.b)Object can encapsulate state for several methods:
class counter: value = 0 def set(self, x): self.value = x def up(self): self.value = self.value + 1 def down(self): self.value = self.value - 1 count = counter() inc, dec, reset = count.up, count.down, count.setHere
inc(),dec()andreset()act like functions which share the same counting variable.From Python Frequently Asked Questions (PSF License Version 2)
How do I copy an object in Python?
In general, try
copy.copy()orcopy.deepcopy()for the general case. Not all objects can be copied, but most can.Some objects can be copied more easily. Dictionaries have a
copy()method:newdict = olddict.copy()
Sequences can be copied by slicing:
new_l = l[:]
From Python Frequently Asked Questions (PSF License Version 2)
How can I find the methods or attributes of an object?
For an instance
xof a user-defined class,dir(x)returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class.From Python Frequently Asked Questions (PSF License Version 2)
How can my code discover the name of an object?
Generally speaking, it can’t, because objects don’t really have names. Essentially, assignment always binds a name to a value; the same is true of
defandclassstatements, but in that case the value is a callable. Consider the following code:>>> class A: ... pass ... >>> B = A >>> a = B() >>> b = a >>> print(b) <__main__.A object at 0x16D07CC> >>> print(a) <__main__.A object at 0x16D07CC>
Arguably the class has a name: even though it is bound to two names and invoked through the name
Bthe created instance is still reported as an instance of classA. However, it is impossible to say whether the instance’s name isaorb, since both names are bound to the same value.Generally speaking it should not be necessary for your code to “know the names” of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.
In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question:
The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn’t really care – so the only way to find out what it’s called is to ask all your neighbours (namespaces) if it’s their cat (object)…
….and don’t be surprised if you’ll find that it’s known by many names, or no name at all!
From Python Frequently Asked Questions (PSF License Version 2)
What’s up with the comma operator’s precedence?
Comma is not an operator in Python. Consider this session:
>>> "a" in "b", "a" (False, 'a')
Since the comma is not an operator, but a separator between expressions the above is evaluated as if you had entered:
("a" in "b"), "a"not:
"a" in ("b", "a")The same is true of the various assignment operators (
=,+=, and so on). They are not truly operators but syntactic delimiters in assignment statements.From Python Frequently Asked Questions (PSF License Version 2)
Is there an equivalent of C’s “?:” ternary operator?
Yes, there is. The syntax is as follows:
[on_true] if [expression] else [on_false] x, y = 50, 25 small = x if x < y else y
Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators:
[expression] and [on_true] or [on_false]
However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the
... if ... else ...form.From Python Frequently Asked Questions (PSF License Version 2)
Is it possible to write obfuscated one-liners in Python?
Yes. Usually this is done by nesting
lambdawithinlambda. See the following three examples, slightly adapted from Ulf Bartelt:from functools import reduce # Primes < 1000 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0, map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000))))) # First 10 Fibonacci numbers print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f), range(10)))) # Mandelbrot set print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+'\n'+y,map(lambda y, Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM, Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro, i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr( 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)) # \___ ___/ \___ ___/ | | |__ lines on screen # V V | |______ columns on screen # | | |__________ maximum of "iterations" # | |_________________ range on y axis # |____________________________ range on x axis
Don’t try this at home, kids!
From Python Frequently Asked Questions (PSF License Version 2)
How do I specify hexadecimal and octal integers?
To specify an octal digit, precede the octal value with a zero, and then a lower or uppercase “o”. For example, to set the variable “a” to the octal value “10” (8 in decimal), type:
>>> a = 0o10 >>> a 8
Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero, and then a lower or uppercase “x”. Hexadecimal digits can be specified in lower or uppercase. For example, in the Python interpreter:
>>> a = 0xa5 >>> a 165 >>> b = 0XB2 >>> b 178
From Python Frequently Asked Questions (PSF License Version 2)
Why does -22 // 10 return -3?
It’s primarily driven by the desire that
i % jhave the same sign asj. If you want that, and also want:i == (i // j) * j + (i % j)
then integer division has to return the floor. C also requires that identity to hold, and then compilers that truncate
i // jneed to makei % jhave the same sign asi.There are few real use cases for
i % jwhenjis negative. Whenjis positive, there are many, and in virtually all of them it’s more useful fori % jto be>= 0. If the clock says 10 now, what did it say 200 hours ago?-190 % 12 == 2is useful;-190 % 12 == -10is a bug waiting to bite.From Python Frequently Asked Questions (PSF License Version 2)
How do I get int literal attribute instead of SyntaxError?
Trying to lookup an
intliteral attribute in the normal manner gives aSyntaxErrorbecause the period is seen as a decimal point:>>> 1.__class__ File "<stdin>", line 1 1.__class__ ^ SyntaxError: invalid decimal literal
The solution is to separate the literal from the period with either a space or parentheses.
>>> 1 .__class__ <class 'int'> >>> (1).__class__ <class 'int'>
From Python Frequently Asked Questions (PSF License Version 2)
How do I convert a string to a number?
For integers, use the built-in
int()type constructor, for example,int('144') == 144. Similarly,float()converts to a floating-point number, for example,float('144') == 144.0.By default, these interpret the number as decimal, so that
int('0144') == 144holds true, andint('0x144')raisesValueError.int(string, base)takes the base to convert from as a second optional argument, soint( '0x144', 16) == 324. If the base is specified as 0, the number is interpreted using Python’s rules: a leading ‘0o’ indicates octal, and ‘0x’ indicates a hex number.Do not use the built-in function
eval()if all you need is to convert strings to numbers.eval()will be significantly slower and it presents a security risk: someone could pass you a Python expression that might have unwanted side effects. For example, someone could pass__import__('os').system("rm -rf $HOME")which would erase your home directory.eval()also has the effect of interpreting numbers as Python expressions, so that, for example,eval('09')gives a syntax error because Python does not allow leading ‘0’ in a decimal number (except ‘0’).From Python Frequently Asked Questions (PSF License Version 2)
How do I convert a number to a string?
For example, to convert the number
144to the string'144', use the built-in type constructorstr(). If you want a hexadecimal or octal representation, use the built-in functionshex()oroct(). For fancy formatting, see the f-strings and Format string syntax sections. For example,"{:04d}".format(144)yields'0144'and"{:.3f}".format(1.0/3.0)yields'0.333'.From Python Frequently Asked Questions (PSF License Version 2)
How do I modify a string in place?
You can’t, because strings are immutable. In most situations, you should simply construct a new string from the various parts you want to assemble it from. However, if you need an object with the ability to modify in-place Unicode data, try using an
io.StringIOobject or thearraymodule:>>> import io >>> s = "Hello, world" >>> sio = io.StringIO(s) >>> sio.getvalue() 'Hello, world' >>> sio.seek(7) 7 >>> sio.write("there!") 6 >>> sio.getvalue() 'Hello, there!' >>> import array >>> a = array.array('w', s) >>> print(a) array('w', 'Hello, world') >>> a[0] = 'y' >>> print(a) array('w', 'yello, world') >>> a.tounicode() 'yello, world'From Python Frequently Asked Questions (PSF License Version 2)
How do I use strings to call functions/methods?
There are various techniques.
The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:
def a(): pass def b(): pass dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs dispatch[get_input()]() # Note trailing parens to call functionUse the built-in function
getattr():import foo getattr(foo, 'bar')()
Note that
getattr()works on any object, including classes, class instances, modules, and so on.This is used in several places in the standard library, like this:
class Foo: def do_foo(self): ... def do_bar(self): ... f = getattr(foo_instance, 'do_' + opname) f()Use
locals()to resolve the function name:def myFunc(): print("hello") fname = "myFunc" f = locals()[fname] f()
From Python Frequently Asked Questions (PSF License Version 2)
Is there an equivalent to Perl’s
chomp()for removing trailing newlines from strings?You can use
S.rstrip("\r\n")to remove all occurrences of any line terminator from the end of the stringSwithout removing other trailing whitespace. If the stringSrepresents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed:>>> lines = ("line 1 \r\n" ... "\r\n" ... "\r\n") >>> lines.rstrip("\n\r") 'line 1 'Since this is typically only desired when reading text one line at a time, using
S.rstrip()this way works well.From Python Frequently Asked Questions (PSF License Version 2)
Is there a
scanf()orsscanf()equivalent?Not as such.
For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the
split()method of string objects and then convert decimal strings to numeric values usingint()orfloat().split()supports an optional “sep” parameter which is useful if the line uses something other than whitespace as a separator.For more complicated input parsing, regular expressions are more powerful than C’s
sscanfand better suited for the task.From Python Frequently Asked Questions (PSF License Version 2)
My program is too slow. How do I speed it up?
That’s a tough one, in general. First, here is a list of things to remember before diving further:
Performance characteristics vary across Python implementations. This FAQ focuses on CPython.
Behaviour can vary across operating systems, especially when talking about I/O or multi-threading.
You should always find the hot spots in your program before attempting to optimize any code (see the
profilemodule).Writing benchmark scripts will allow you to iterate quickly when searching for improvements (see the
timeitmodule).It is highly recommended to have good code coverage (through unit testing or any other technique) before potentially introducing regressions hidden in sophisticated optimizations.
That being said, there are many tricks to speed up Python code. Here are some general principles which go a long way towards reaching acceptable performance levels:
Making your algorithms faster (or changing to faster ones) can yield much larger benefits than trying to sprinkle micro-optimization tricks all over your code.
Use the right data structures. Study documentation for the Built-in Types and the
collectionsmodule.When the standard library provides a primitive for doing something, it is likely (although not guaranteed) to be faster than any alternative you may come up with. This is doubly true for primitives written in C, such as builtins and some extension types. For example, be sure to use either the
list.sort()built-in method or the relatedsorted()function to do sorting (and see the Sorting Techniques for examples of moderately advanced usage).Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability).
If you have reached the limit of what pure Python can allow, there are tools to take you further away. For example, Cython can compile a slightly modified version of Python code into a C extension, and can be used on many different platforms. Cython can take advantage of compilation (and optional type annotations) to make your code significantly faster than when interpreted. If you are confident in your C programming skills, you can also write a C extension module yourself.
See also
The wiki page devoted to performance tips.
From Python Frequently Asked Questions (PSF License Version 2)
How do I convert between tuples and lists?
The type constructor
tuple(seq)converts any sequence (actually, any iterable) into a tuple with the same items in the same order.For example,
tuple([1, 2, 3])yields(1, 2, 3)andtuple('abc')yields('a', 'b', 'c'). If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to calltuple()when you aren’t sure that an object is already a tuple.The type constructor
list(seq)converts any sequence or iterable into a list with the same items in the same order. For example,list((1, 2, 3))yields[1, 2, 3]andlist('abc')yields['a', 'b', 'c']. If the argument is a list, it makes a copy just likeseq[:]would.From Python Frequently Asked Questions (PSF License Version 2)
What’s a negative index?
Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of
seq[-n]as the same asseq[len(seq)-n].Using negative indices can be very convenient. For example
S[:-1]is all of the string except for its last character, which is useful for removing the trailing newline from a string.From Python Frequently Asked Questions (PSF License Version 2)
How do I iterate over a sequence in reverse order?
Use the
reversed()built-in function:for x in reversed(sequence): ... # do something with x ...This returns a reverse iterator without modifying your original sequence.
From Python Frequently Asked Questions (PSF License Version 2)
How do you remove duplicates from a list?
See the Python Cookbook for a long discussion of many ways to do this:
If you don’t mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go:
if mylist: mylist.sort() last = mylist[-1] for i in range(len(mylist)-2, -1, -1): if last == mylist[i]: del mylist[i] else: last = mylist[i]If all elements of the list may be used as set keys (that is, they are all hashable) this is often faster:
mylist = list(set(mylist))
This converts the list into a set, thereby removing duplicates, and then back into a list.
From Python Frequently Asked Questions (PSF License Version 2)
How do you remove multiple items from a list?
As with removing duplicates, explicitly iterating in reverse with a delete condition is one possibility. However, it is easier and faster to use slice replacement with an implicit or explicit forward iteration. Here are three variations:
mylist[:] = filter(keep_function, mylist) mylist[:] = (x for x in mylist if keep_condition) mylist[:] = [x for x in mylist if keep_condition]
The list comprehension may be fastest.
From Python Frequently Asked Questions (PSF License Version 2)
Related topics
Where these questions come from
Open-source collections on GitHub, official documentation, a government careers service and the Stack Exchange sites, each used under its own licence. The answers are the authors' own words: we only tidy the layout, leave out pictures, and cut a long answer with a link to the rest. Stack Exchange answers stay under CC BY-SA and name who asked and who answered.
- Python Frequently Asked Questionsby the Python Software FoundationPSF License Version 2, Copyright © 2001 Python Software Foundation; All Rights Reserved76 questions
- What the f*ck Python!by Satwik Kansal and contributorsWTFPL, © 2018 Satwik Kansal63 questions
- Stack Overflowby its contributors, named on each answerCC BY-SA13 questions