Categories
Python Answers

How to create a list of random numbers without duplicates with Python?

Sometimes, we want to create a list of random numbers without duplicates with Python.

In this article, we’ll look at how to create a list of random numbers without duplicates with Python.

How to create a list of random numbers without duplicates with Python?

To create a list of random numbers without duplicates with Python, we can use the random.sample method.

For instance, we write:

import random

l = random.sample(range(100), 10)
print(l)

We call random.sample with the range of numbers to generate and the number of random numbers to generate respectively.

We generate numbers between 0 and 99 with range(100).

And then we assign the returned list to l.

Therefore, l is something like [34, 77, 97, 83, 22, 28, 74, 93, 59, 87].

Conclusion

To create a list of random numbers without duplicates with Python, we can use the random.sample method.

Categories
Python Answers

How to remove elements that have consecutive duplicates with Python?

Sometimes, we want to remove elements that have consecutive duplicates with Python.

In this article, we’ll look at how to remove elements that have consecutive duplicates with Python.

How to remove elements that have consecutive duplicates with Python?

To remove elements that have consecutive duplicates with Python, we can use the groupby function from the itertools module and list comprehension.

For instance, we write:

from itertools import groupby

l = [1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 5, 1, 2]

no_dups = [x[0] for x in groupby(l)]
print(no_dups)

We call groupby with l to group the elements in l by their values and return a new iterable with the grouped items in their own list.

Then we get the first element from each group with x[0] and assign the returned list to no_dups.

Therefore, no_dups is [1, 2, 3, 4, 5, 1, 2].

Conclusion

To remove elements that have consecutive duplicates with Python, we can use the groupby function from the itertools module and list comprehension.

Categories
Python Answers

How to open multiple files using “with open” in Python?

Sometimes, we want to open multiple files using "with open" in Python.

In this article, we’ll look at how to open multiple files using "with open" in Python.

How to open multiple files using "with open" in Python?

To open multiple files using "with open" in Python, we can separate each file with a comma.

For instance, we write:

with open('foo.txt', 'r') as a, open('bar.txt', 'r') as b:
    print(a.readlines())
    print(b.readlines())

We call open with 'r' to open foo.txt and bar.txt.

Then we call readlines on each file to read the content of them.

Therefore, is foo.txt has:

foo

And bar.txt has:

bar

Then we see:

['foo']
['bar']

printed.

Conclusion

To open multiple files using "with open" in Python, we can separate each file with a comma.

Categories
Python Answers

How to make a copy of dicts on assignment with Python?

Sometimes, we want to make a copy of dicts on assignment with Python.

In this article, we’ll look at how to make a copy of dicts on assignment with Python.

How to make a copy of dicts on assignment with Python?

To make a copy of dicts on assignment with Python, we can call the copy method.

For instance, we write:

a = {'foo': 2}
b = a.copy()
b['bar'] = 3

print(a)
print(b)

We call a.copy to make a copy of a and assign it to b.

Then we added a new entry with key 'bar' to b.

Since we made a copy of a and assigned that to b, a is unchanged when we changed b.

So a is {'foo': 2} and b is {'foo': 2, 'bar': 3}.

Conclusion

To make a copy of dicts on assignment with Python, we can call the copy method.

Categories
Python Answers

How to use quotation marks inside quotation marks with Python?

Sometimes, we want to use quotation marks inside quotation marks with Python.

In this article, we’ll look at how to use quotation marks inside quotation marks with Python.

How to use quotation marks inside quotation marks with Python?

To use quotation marks inside quotation marks with Python, we can wrap single quotes around double quotes and vice versa, or we can escape single quotes in single quotes or escape double quotes in double quotes.

We can also use quotes within triple quoted strings.

For instance, we write:

print('"A word that needs quotation marks"')
print("'A word that needs quotation marks'")
print("\"A word that needs quotation marks\"")
print('\'A word that needs quotation marks\'')
print("""\"A word that needs quotation marks\"""")

In the first 2 strings, we wrap single quotes around double quotes and vice versa.

We escaped the quotes in the last 3 strings.

Therefore, we should see:

"A word that needs quotation marks"
'A word that needs quotation marks'
"A word that needs quotation marks"
'A word that needs quotation marks'
"A word that needs quotation marks"

printed.

Conclusion

To use quotation marks inside quotation marks with Python, we can wrap single quotes around double quotes and vice versa, or we can escape single quotes in single quotes or escape double quotes in double quotes.