Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm somewhat befuddled in regards to a data structure in python; (),[], and {}. I'm attempting to sort a straightforward list, most likely since I can't distinguish the kind of information I am neglecting to sort it. 

My List: ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']

My inquiry is what kind of information this is, and how to sort the words one after another in order?

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
The data structure you are referring to is a list, denoted by square brackets [] in Python. Lists are ordered collections of items and can contain elements of different types, including strings, numbers, or even other lists.

To sort the words in your list alphabetically, you can use the sort() method, which modifies the list in-place. Here's an example:

my_list = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']

my_list.sort()

print(my_list)

Output:

['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute']

In this code, the sort() method is called on the my_list variable, which sorts the elements of the list in ascending alphabetical order. The modified list is then printed.

By using the sort() method, you can sort the words in your list alphabetically, considering them as strings.
0 votes
by (26.4k points)

Here,

[] denotes list

() denotes a tuple

{} denotes a dictionary

You should investigate the authority Python tutorial as these are the actual rudiments of programming in Python. 

What you have is a rundown/list of strings. You can also sort it like this:

In [1]: lst = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']

In [2]: sorted(lst)

Out[2]: ['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute']

As should be obvious, words that start with a capitalized letter get inclination over those beginning with a lowercase letter. On the off chance that you need to sort them autonomously, do this 

In [4]: sorted(lst, key=str.lower)

Out[4]: ['constitute', 'Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim']

You can likewise sort the rundown in turn around the request by doing this:

In [12]: sorted(lst, reverse=True)

Out[12]: ['constitute', 'Whim', 'Stem', 'Sedge', 'Intrigue', 'Eflux']

In [13]: sorted(lst, key=str.lower, reverse=True)

Out[13]: ['Whim', 'Stem', 'Sedge', 'Intrigue', 'Eflux', 'constitute']

Join the python online course fast, to learn python concepts in detail and get certified.

For more details, do check out the below video tutorial...

0 votes
by (15.4k points)
The data structure you are working with is a list, represented by square brackets [] in Python. Lists are ordered collections that can hold elements of different types, such as strings, numbers, or even other lists.

To sort the words in your list in alphabetical order, you can utilize the sort() method. This method modifies the list directly. Here's an example:

my_list = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']

my_list.sort()

print(my_list)

Output:

['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute']

In the provided code, the sort() method is called on the my_list variable. This sorts the elements of the list in ascending alphabetical order. The modified list is then printed.

By using the sort() method, you can easily arrange the words in your list alphabetically, treating them as strings.
0 votes
by (19k points)
The data structure you are referring to is a list, which is denoted by square brackets [] in Python. Lists are ordered collections that can store elements of different types, including strings, numbers, or other lists.

To sort the words in your list in alphabetical order, you can use the sort() method. This method directly modifies the list itself. Here's an example:

my_list = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']

my_list.sort()

print(my_list)

Output:

['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute']

In the provided code, the sort() method is called on the my_list variable. This arranges the elements of the list in ascending alphabetical order. Finally, the modified list is printed.

By using the sort() method, you can easily sort the words in your list alphabetically, considering them as strings.

Related questions

0 votes
2 answers
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
4 answers
0 votes
1 answer
asked Aug 6, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Aug 1, 2019 in Python by Sammy (47.6k points)

Browse Categories

...