Most voted questions in Python

0 votes
1 answer

How to sort a list of strings?

Basic answer: mylist = ["b", "C", "A"] mylist.sort() This modifies ...READ MORE

Jun 4, 2018 in Python by aryya
• 7,450 points
1,260 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

Jun 4, 2018 in Python by aryya
• 7,450 points
1,104 views
0 votes
1 answer

How to read/process command line arguments?

The canonical solution in the standard library ...READ MORE

Jun 4, 2018 in Python by aryya
• 7,450 points
609 views
0 votes
1 answer

a basic question about “while true”

while True means loop forever. The while ...READ MORE

Jun 4, 2018 in Python by aryya
• 7,450 points
5,470 views
0 votes
1 answer

Enlarging a list using functions using append() and extend()

Let me illustrate this with an example ...READ MORE

Jun 1, 2018 in Python by Nietzsche's daemon
• 4,260 points
393 views
0 votes
1 answer

Extract all characters of a string

Convert it to a list -  s = ...READ MORE

Jun 1, 2018 in Python by Nietzsche's daemon
• 4,260 points
621 views
0 votes
2 answers

Obtaining a value when given a key in python dicionaries

Yes you can check below code dictionary = ...READ MORE

Nov 25, 2021 in Python by Suhas
607 views
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

Jun 1, 2018 in Python by george
• 200 points
1,272 views
0 votes
1 answer

What exactly does the .join() method do?

Look carefully at your output: 5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5 ^ ...READ MORE

May 31, 2018 in Python by charlie_brown
• 7,720 points
410 views
0 votes
1 answer

Multiple line comment in Python

Try this ''' This is a multiline comment. I can ...READ MORE

May 31, 2018 in Python by charlie_brown
• 7,720 points
382 views
0 votes
1 answer

Number of days between dates in Python

You can use the date module to ...READ MORE

May 30, 2018 in Python by Nietzsche's daemon
• 4,260 points
427 views
0 votes
1 answer

Join all strings in a list of strings

s = " ".join(myList) #here " " ...READ MORE

May 30, 2018 in Python by Nietzsche's daemon
• 4,260 points
537 views
0 votes
1 answer

Replacements for switch statement in Python?

You could use a dictionary: def f(x): ...READ MORE

May 29, 2018 in Python by aryya
• 7,450 points
530 views
0 votes
1 answer

How do you get the logical xor of two variables in Python?

If you're already normalizing the inputs to ...READ MORE

May 29, 2018 in Python by aryya
• 7,450 points
10,479 views
0 votes
1 answer

Using Lists and Tuples in Python

if you are familiar with C programming, ...READ MORE

May 29, 2018 in Python by Nietzsche's daemon
• 4,260 points
731 views
0 votes
1 answer

Sorting a list of strings in Python

Use the sort function mylist.sort() READ MORE

May 29, 2018 in Python by Nietzsche's daemon
• 4,260 points
449 views
0 votes
1 answer

Looping over a string in python

for c in "string": ...READ MORE

May 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
462 views
0 votes
1 answer

Comparing strings in Python

Convert both the strings to either uppercase ...READ MORE

May 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
618 views
0 votes
2 answers

Splitting a Python String

The split() method in Python returns a ...READ MORE

Oct 3, 2018 in Python by anonymous
500 views
0 votes
1 answer

Size of an object in Python

Use sys.getsizeof() function: >>> import sys >>> s = ...READ MORE

May 25, 2018 in Python by Nietzsche's daemon
• 4,260 points
839 views
0 votes
1 answer

Difference between two lists in python

difference = list(set(list1) - set(list2)) READ MORE

May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
2,600 views
0 votes
1 answer

Create and open a file in Python

file = open('text.txt', 'w+) READ MORE

May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
708 views
0 votes
1 answer

How can I get a list of locally installed Python modules?

Solution My 50 cents for getting a pip freeze-like ...READ MORE

May 24, 2018 in Python by charlie_brown
• 7,720 points
1,034 views
0 votes
1 answer

Python set Union and set Intersection operate differently?

When you do set() you are creating an empty ...READ MORE

May 24, 2018 in Python by charlie_brown
• 7,720 points
821 views
0 votes
1 answer

Traverse a list in reverse

for item in my_list[::-1]: ...READ MORE

May 23, 2018 in Python by Nietzsche's daemon
• 4,260 points
439 views
0 votes
1 answer

Inverting a python dictionary

inv_dict = {value: key for key, value ...READ MORE

May 23, 2018 in Python by Nietzsche's daemon
• 4,260 points
344 views
0 votes
1 answer

How to sort Counter by value using python?

Use the Counter.most_common() method, it'll sort the items for you: >>> ...READ MORE

May 23, 2018 in Python by charlie_brown
• 7,720 points
10,063 views
0 votes
1 answer

What does ' -> ' mean in Python function definitions?

It's a function annotation. In more detail, Python 2.x ...READ MORE

May 23, 2018 in Python by charlie_brown
• 7,720 points
662 views
0 votes
1 answer

Looping over two lists at once

You have to use the zip() function: for ...READ MORE

May 15, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,560 views
0 votes
1 answer

Reading Unix Timestamp in Python

import datetime print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S')) That is the cleanest way ...READ MORE

May 15, 2018 in Python by Nietzsche's daemon
• 4,260 points
794 views
0 votes
1 answer

Can someone explain the behaviour of increment and decrement operators in python

down voteaccepted ++ is not an operator. It is ...READ MORE

May 15, 2018 in Python by aryya
• 7,450 points
1,522 views
0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

950down voteaccepted It's because any iterable can be ...READ MORE

May 15, 2018 in Python by aryya
• 7,450 points
710 views
0 votes
1 answer

Python Setup file

 setup.py tells you that the module or the ...READ MORE

May 14, 2018 in Python by Nietzsche's daemon
• 4,260 points
302 views
0 votes
1 answer

Difference between '==' and 'is'

'==' checks for the equality of the ...READ MORE

May 14, 2018 in Python by Nietzsche's daemon
• 4,260 points
578 views
0 votes
2 answers

Do-while loop in Python

A more pythonic way would be this: ``` while ...READ MORE

Jul 6, 2020 in Python by Composer3
1,161 views
0 votes
1 answer

Increment operators in Python

Python has no unary operators. So use ...READ MORE

May 12, 2018 in Python by Hamartia's Mask
• 1,580 points
814 views
0 votes
1 answer

Remove duplicate elements in a list

Here is the code for this - list(set((list_of_numbers) For ...READ MORE

May 12, 2018 in Python by Hamartia's Mask
• 1,580 points
621 views
0 votes
1 answer

Deleting a list element by value

Use the remove() function: >>> p = [1, ...READ MORE

May 12, 2018 in Python by Hamartia's Mask
• 1,580 points
440 views
0 votes
1 answer

How to exit from Python without traceback?

shutil has many methods you can use. One ...READ MORE

May 11, 2018 in Python by charlie_brown
• 7,720 points
645 views
0 votes
2 answers

Manually raising (throwing) an exception in Python

Exception handling issues in python can easily ...READ MORE

Jan 18, 2019 in Python by nick
• 140 points
17,148 views
0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

Mar 27, 2019 in Python by rajesh
• 1,270 points
981 views
0 votes
1 answer

Using quotes in python

Both the conventions for enclosing strings are ...READ MORE

May 9, 2018 in Python by Nietzsche's daemon
• 4,260 points
396 views
0 votes
1 answer

Uninstalling python packages

It is best to install pip in ...READ MORE

May 9, 2018 in Python by Nietzsche's daemon
• 4,260 points
324 views
0 votes
1 answer

Numpy: Multiplying large arrays with dtype=int8 is SLOW

Unfortunately, the "engine" behind the scenes is BLAS, ...READ MORE

May 9, 2018 in Python by charlie_brown
• 7,720 points
1,054 views
0 votes
1 answer

OSX 10.11 with py2app?

I'm not sure if this is your ...READ MORE

May 9, 2018 in Python by charlie_brown
• 7,720 points
1,076 views
0 votes
1 answer

SKLearn NMF Vs Custom NMF

The choice of the optimizer has a ...READ MORE

May 9, 2018 in Python by charlie_brown
• 7,720 points
1,440 views
0 votes
1 answer

Exiting a python program

Use the quit() function. This function can ...READ MORE

May 8, 2018 in Python by Nietzsche's daemon
• 4,260 points
308 views
0 votes
1 answer

Type checking in Python

Use the type() function with the variable ...READ MORE

May 8, 2018 in Python by Nietzsche's daemon
• 4,260 points
323 views
0 votes
1 answer

Reversing strings in Python

The fastest way to reverse string is ...READ MORE

May 7, 2018 in Python by Nietzsche's daemon
• 4,260 points
402 views
0 votes
1 answer

Python comments

Use docstrings for multi-line commenting -  """ Line 1 Line ...READ MORE

May 7, 2018 in Python by Nietzsche's daemon
• 4,260 points
515 views